我對Android和Java相當陌生,下面的代碼是通過IllegalStateException
。通過放置打印日誌語句,我已確定代碼使其到線:Java Android - IllegalStateException,無法實例化httpost
"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.[email protected] 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)
使用調試器,並添加一個手錶「主機+參數」,看看它的你希望它是什麼。 –
該網址是正確的,它仍然無法正常工作。 –