- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 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/
我对 Android 和 Java 相当陌生,下面的代码是通过 IllegalStateException 实现的。通过放置打印日志语句,我确定代码已达到以下行: "HttpPost post =
EDIT: AsyncTask was called lots of times to update status in Server and at the same time, my app tri
关于这个问题已经有很多问题了,但是尽管查看了其中的一些问题,我还是很难看出问题出在哪里。当我提交以下表格时,模型为空。关于我在这里做错了什么有什么建议吗? 模型(部分): public class I
我正在尝试让我的 Android 应用程序与我的 Spring REST Web 服务对话。我正在从 android 制作 httpost ,如下代码。但是我收到下面的异常。无论我为帖子请求的内容类型
我是一名优秀的程序员,十分优秀!