gpt4 book ai didi

Java Android - IllegalStateException,无法实例化 httpost

转载 作者:行者123 更新时间:2023-12-02 06:37:28 25 4
gpt4 key购买 nike

我对 Android 和 Java 相当陌生,下面的代码是通过 IllegalStateException 实现的。通过放置打印日志语句,我确定代码已达到以下行:

 "HttpPost post = new HttpPost(string+params)" (makes it to "Point 1")

我不明白为什么会出现错误。发送到 HttpPost 的 URL 有效。

注意,我尝试使用注释掉的 HttpGet 行代替,但出现了相同的错误。

public String ReadQuery(String SQL)
{

String host = "http://web.engr.illinois.edu/~dyel-net/readquery.php";


DefaultHttpClient httpclient = new DefaultHttpClient();

try
{



HttpPost httpPost = new HttpPost(host);
httpPost.addHeader("Accept", "text/plain");
Log.w("DEBUGGING PRINT", "point 1");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user", username));
nvps.add(new BasicNameValuePair("pw", password));
nvps.add(new BasicNameValuePair("sql", SQL));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
Log.w("DEBUGGING PRINT", "point 2");
Log.w("DEBUGGING PRINT", httpPost.getURI().toString());
Log.w("DEBUGGING PRINT", httpPost.getEntity().toString());
HttpResponse response = httpclient.execute(httpPost);
Log.w("DEBUGGING PRINT", "point 3");
HttpEntity entity = response.getEntity();
Log.w("DEBUGGING PRINT", "point 4");
String htmlResponse = EntityUtils.toString(entity);
Log.w("DEBUGGING PRINT", "point 5");
return htmlResponse;

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

return "ERROR";

我的错误:

10-21 13:39:09.472: W/DEBUGGING PRINT(866): point 1 10-21 13:39:09.582: W/DEBUGGING PRINT(866): point 2 10-21 13:39:09.582: W/DEBUGGING PRINT(866): http://web.engr.illinois.edu/~dyel-net/readquery.php? 10-21 13:39:09.616: W/DEBUGGING PRINT(866): org.apache.http.params.BasicHttpParams@4186af10 10-21 13:39:22.792: D/AndroidRuntime(866): Shutting down VM 10-21 13:39:22.802: W/dalvikvm(866): threadid=1: thread exiting with uncaught exception (group=0x41465700) 10-21 13:39:23.713: E/AndroidRuntime(866): FATAL EXCEPTION: main 10-21 13:39:23.713: E/AndroidRuntime(866): java.lang.IllegalStateException: Could not execute method of the activity 10-21 13:39:23.713: E/AndroidRuntime(866): at android.view.View$1.onClick(View.java:3633) 10-21 13:39:23.713: E/AndroidRuntime(866): at android.view.View.performClick(View.java:4240) 10-21 13:39:23.713: E/AndroidRuntime(866): at android.view.View$PerformClick.run(View.java:17721) 10-21 13:39:23.713: E/AndroidRuntime(866): at android.os.Handler.handleCallback(Handler.java:730) 10-21 13:39:23.713: E/AndroidRuntime(866): at android.os.Handler.dispatchMessage(Handler.java:92) 10-21 13:39:23.713: E/AndroidRuntime(866): at android.os.Looper.loop(Looper.java:137) 10-21 13:39:23.713: E/AndroidRuntime(866): at android.app.ActivityThread.main(ActivityThread.java:5103) 10-21 13:39:23.713: E/AndroidRuntime(866): at java.lang.reflect.Method.invokeNative(Native Method) 10-21 13:39:23.713: E/AndroidRuntime(866): at java.lang.reflect.Method.invoke(Method.java:525) 10-21 13:39:23.713: E/AndroidRuntime(866): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

最佳答案

 HttpPost post = new HttpPost(host+params);
//HttpGet get = new HttpGet(host+params);

这不是使用 HttpClient 发出 POST 请求的正确方法。 HTTP GET 请求和 HTTP POST 请求的工作方式不同。在 HTTP GET 请求中,您可以将数据作为 url 的一部分发送,例如 http://mysite.com?feild1=data1&feild2=data2,但是 HTTP POST 请求具有 请求消息正文 code> 您需要将数据写入服务器的位置。

尝试以下代码:

HttpPost httpPost = new HttpPost("http://targethost/login");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user", username));
nvps.add(new BasicNameValuePair("pw", password));
nvps.add(new BasicNameValuePair("sql", SQL));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);

关于Java Android - IllegalStateException,无法实例化 httpost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19483961/

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