2013-10-20 67 views
0

我對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)

+0

使用調試器,並添加一個手錶「主機+參數」,看看它的你希望它是什麼。 –

+0

該網址是正確的,它仍然無法正常工作。 –

回答

2
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請求有request message body,您需要將數據寫入服務器。

試試下面的代碼:

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); 
+0

請顯示錯誤。寫入異常 – Sage

+0

我仍然收到「HttpResponse response2 = httpclient.execute(httpPost);」的錯誤,無法執行該活動的方法。 –

+0

我一再問你這個問題。什麼錯誤,它顯示什麼消息? – Sage

相關問題