2012-06-21 44 views
0

在我的應用程序中,我需要發送各種POST請求到服務器。其中一些請求有答覆,其他請求沒有答覆。Android:發送沒有迴應的帖子

這是我用來發送請求的代碼:

private static final String TAG = "Server"; 
private static final String PATH = "http://10.0.0.2:8001/data_connection"; 
private static HttpResponse response = null; 
private static StringEntity se = null; 
private static HttpClient client; 
private static HttpPost post = null; 
public static String actionKey = null; 

public static JSONObject sendRequest(JSONObject req) { 
    try { 
     client = new DefaultHttpClient(); 

     actionKey = req.getString("actionKey"); 
     se = new StringEntity(req.toString()); 
     se.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING, "application/json")); 
     se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
     post = new HttpPost(PATH); 
     post.setEntity(se); 

     Log.d(TAG, "http request is being sent"); 
     response = client.execute(post); 
     Log.d(TAG, "http request was sent"); 

     if (response != null) { 
      InputStream in = response.getEntity().getContent(); 
      String a = convertFromInputStream(in); 
      in.close(); 
      return new JSONObject(a); 
     } 

    } catch (UnsupportedEncodingException e) { 
     Log.d(TAG, "encoding request to String entity faild!"); 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     Log.d(TAG, "executing the http POST didn't work"); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     Log.d(TAG, "executing the http POST didn't work"); 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     Log.d(TAG, "no ActionKey"); 
     e.printStackTrace(); 
    } 
    return null; 
} 

private static String convertFromInputStream(InputStream in) 
     throws IOException { 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = br.readLine()) != null) { 
     sb.append(line); 
    } 
    return (sb.toString()); 
} 


這是發送請求的 AsyncTask類的代碼:

class ServerRequest extends AsyncTask<JSONObject, Void, JSONObject> { 

    @Override 
    protected JSONObject doInBackground(JSONObject... params) { 
     JSONObject req = params[0]; 
     JSONObject response = Server.sendRequest(req); 
     return response; 
    } 

    @Override 
    protected void onPostExecute(JSONObject result) { 
     // HANDLE RESULT 
     super.onPostExecute(result); 
    } 
} 


我的問題當服務器沒有返回響應時開始。即使完成工作, AsyncTask線程仍保持打開狀態,因爲 HTTPClient從不關閉連接。

有沒有辦法不等待迴應?這肯定會給服務器增加很多開銷,因爲所有試圖連接到它的Android應用程序都會使連接保持活動狀態,並且可能會導致應用程序本身出現很多問題。

基本上,我正在尋找的是一種方法,將允許我發送到POST消息並在請求發送後立即終止連接,因爲沒有響應以我的方式發送。

+1

這並沒有什麼意義。爲什麼沒有迴應?除非你長時間輪詢,否則應該有一些迴應,即使它沒有一個機構,只是一個狀態碼。如果你沒有得到/等待迴應,你怎麼能確定服務器甚至收到你的請求? –

回答

0

只是,集ConnectionTimeOut與HttpClient的對象,(代碼是你在你的情況瞭解也可能是不同的)

int TIMEOUT_MILLISEC = 30000; 
HttpParams httpParams = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); 
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
HttpClient httpclient = new DefaultHttpClient(httpParams); 
HttpPost httppost = new HttpPost(url); 
httppost.addHeader("Content-Type", "application/json"); 

現在,它將後TimeoOut您定義終止連接。但可以肯定,這將拋出TimeOutException所以你必須處理這個異常在HttpRequest的 ..(使用嘗試-catch)

編輯:或者你可以使用HttpRequestExecutor類。

HttpRequestExecutor類包org.apache.http.protocol

protected boolean canResponseHaveBody (HttpRequest request, HttpResponse response) 

決定響應是否配備了一個實體的。該類中的實現基於RFC 2616.未知的方法和響應代碼應該表示對實體的響應。 派生的執行程序可以覆蓋此方法來處理RFC 2616中未指定的方法和響應代碼。