2012-06-21 68 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中未指定的方法和响应代码。