gpt4 book ai didi

java - Java HTTP 请求错误

转载 作者:行者123 更新时间:2023-11-30 09:13:49 25 4
gpt4 key购买 nike

我尝试通过单击按钮时的 HTTP 发布请求上传姓名和乐谱,但出现奇怪的错误。这是我的代码。

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bMenu:
Intent i = new Intent(ShowScore.this, Menu.class);
startActivity(i);
break;
case R.id.bUpload:
postData();
break;
}
}

private void postData() {

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.ratemyplays.com/form.php");

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", "Steve"));
nameValuePairs.add(new BasicNameValuePair("score", "24"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
// HttpResponse response = httpclient.execute(httppost);

// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);

// Just display the response back
displayToastMessage(responseBody);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

private void displayToastMessage(String responseBody) {
// TODO Auto-generated method stub
}

这是我的错误

Invalid layout of java.lang.String at value

A fatal error has been detected by the Java Runtime Environment:

Internal Error (javaClasses.cpp:136), pid=9736, tid=10668 fatal error: Invalid layout of preloaded class

JRE version: (7.0_40-b43) (build ) Java VM: Java HotSpot(TM) 64-Bit Server VM (24.0-b56 mixed mode windows-amd64 compressed oops) Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

最佳答案

如果您查看堆栈跟踪,您可能会发现 NetworkOnMainThreadException。自 Android 3.x 及更高版本以来,您无法在与 UI 相同的线程上运行网络操作。

总而言之,您需要使用 ASyncTask

来源:Android IllegalStateException: Could not execute method of the activity (我自己测试了代码并跟踪堆栈跟踪发现了这个答案)

关于java - Java HTTP 请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20841581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com