2013-01-02 46 views
0

我有一個Android調用Web服務的方法,如下所述。httpclient.execute沒有給出結果既不例外

現在,當我的班級調用此方法時,我無法看到任何異常,只有到登錄後纔會出現System.out.println("entered into call service method 2");。我可以看到在logcat 狀態response = httpclient.execute(httppost);不工作,並且System.out.println("entered into call service method 3");沒有顯示在logcat也沒有任何異常。 任何想法爲什麼這樣?如何解決它?

  public void callService() throws Exception { 
     System.out.println("entered into call service method"); 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://localhost:81/a.php"); 
     HttpResponse response; 
     System.out.println("entered into call service method 1"); 
     try{ 
      System.out.println("entered into call service method 2"); 
      **response = httpclient.execute(httppost);** 
      System.out.println("entered into call service method 3"); 
+0

你能張貼的callService方法整個源? – varevarao

+0

感謝他們工作的錯誤是使用10.0.2.2而不是本地主機如果使用模擬器 – munish

回答

1

您是否在單獨的Thread/AsyncTask中調用此函數?如果請求經常在您的應用中使用,我還建議考慮使用服務。

下面是一個POST方法,爲我的作品,但不要忘了asyncronously莫名其妙地叫它:

public void executePost(String url, List<NameValuePair> postParams) { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 
    InputStream is = null; 
    try { 
     httppost.setEntity(new UrlEncodedFormEntity(postParams)); 
     HttpResponse response = httpclient.execute(httppost); 
     if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
      HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       is = entity.getContent(); 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       int inChar; 
       while ((inChar = is.read()) != -1) { 
        bos.write(inChar); 
       } 

       String resp = bos.toString(); 
       // report back the resp e.g. via LocalBroadcast message 
      } else { 
       // report back e.g. via LocalBroadcast message 
      } 
     } 
     else { 
      // report back e.g. via LocalBroadcast message 
     } 
    } catch (Exception e) { 
      // report back the exception e.g. via LocalBroadcast message 
     // exception message: e.getMessage() 
    } finally { 
     if (is != null) { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+1

其工作的錯誤是使用10.0.2.2而不是本地主機如果使用模擬器 – munish