2014-07-24 153 views
0

我有一個奇怪的問題,從Android設備調用我的Web服務。關閉Web服務請求的問題

我第一次調用web服務(REST)時,ws調用在大約100 ms內執行。而如果我在幾秒鐘後重試,通話時間約爲20秒。

如果我執行許多後續調用Web服務,使用POSTMAN(用於測試)我看不到任何問題:所有請求都在大約200毫秒內處理。

所以我認爲問題出在我的Android客戶端。

下面有我使用調用WS代碼:

public class DownloadTask extends AsyncTask<Void, Void, Void>{ 

    private Context context; 

    public DownloadTask(Context context){ 
     this.context = context; 
     contactList = new ArrayList<Contact>(); 
    } 

    @Override 
    protected void onPreExecute() { 
     if (mCallbacks != null) { 
      mCallbacks.onPreExecute(); 
      } 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     if(Utils.isOnline(context)) 
     { 
      int resCode = -1; 

      String wsURI = "myuri..."; 
      HttpGet request = new HttpGet(wsURI); 
      request.setHeader("Content-Type", "application/json"); 

      try{ 
       // Send request to service 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpResponse response = httpClient.execute(request); 
       resCode = response.getStatusLine().getStatusCode(); 

       if(resCode == 200){ 
        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
        String line=""; 
        StringBuffer returnFromServer = new StringBuffer(); 

        while ((line=in.readLine())!=null) 
        { 
         returnFromServer.append(line); 
        } 

        Gson gson = new GsonBuilder().create();      
        contactList = gson.fromJson(returnFromServer.toString(), new TypeToken<ArrayList<Contact>>(){}.getType()); 
       } 
      }catch(Exception ex){ 
       ex.printStackTrace(); 
      } 

     } 

     return null;   
    } 
} 

編輯: 我試圖在調試運行我的應用程序,我可以看到,這個問題似乎是在這裏:

HttpResponse response = httpClient.execute(request);

它第一次立即執行,而第二次(接近第一次)呼叫持續約10/20秒。

+0

我可以看到URL ... – User

+0

沒有,對不起@Mobile他們是士兵 – GVillani82

+0

然後嘗試以下urls..1)http://sarangasl.blogspot.in/2011/10/android- web-service-access-tutorial.html 2)http://ihofmann.wordpress.com/2013/01/23/android-sending-post-requests-with-parameters/ – User

回答

0

我嘗試了一些解決方案建議here,沒有結果。最後我儘量避免使用DefaultHttpClientAndroidHttpClient,贊成HttpURLConnection這解決了問題

低於doInBackground方法,即使用HttpURLConnection

protected Void doInBackground(Void... params) { 

    if(Utils.isOnline(context)) 
    { 
     String wsURI = "myUriAddress..."; 
     InputStream inp=null; 

     try{ 
      URL url = new URL(wsURI); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.addRequestProperty("Content-Type", "application/json"); 

      inp = new BufferedInputStream(urlConnection.getInputStream()); 

      if(urlConnection.getResponseCode() == 200){ 
       BufferedReader in = new BufferedReader(new InputStreamReader(inp, "UTF-8")); 
       String line=""; 
       StringBuffer returnFromServer = new StringBuffer(); 

       while ((line=in.readLine())!=null) 
       { 
        returnFromServer.append(line); 
       } 

       in.close(); 
       inp.close(); 

       Log.i("doInBackground",""+returnFromServer.toString()); 

       Gson gson = new GsonBuilder().create();      
       contactList = gson.fromJson(returnFromServer.toString(), new TypeToken<ArrayList<Contact>>(){}.getType()); 
      } 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 

    } 
    return null;    
}