Java Android:发送没有响应的帖子

Java Android:发送没有响应的帖子,java,android,http-post,apache-httpclient-4.x,Java,Android,Http Post,Apache Httpclient 4.x,在我的应用程序中,我需要向服务器发送各种POST请求。其中一些请求有响应,而其他请求没有响应 这是我用来发送请求的代码: 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 StringEnti

在我的应用程序中,我需要向服务器发送各种
POST
请求。其中一些请求有响应,而其他请求没有响应

这是我用来发送请求的代码:

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);
    }
}
class ServerRequest扩展了异步任务{
@凌驾
受保护的JSONObject doInBackground(JSONObject…参数){
JSONObject req=params[0];
JSONObject响应=Server.sendRequest(req);
返回响应;
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
//处理结果
super.onPostExecute(结果);
}
}

当服务器不返回响应时,我的问题就开始了。即使在工作完成后,
AsyncTask
线程仍保持打开状态,因为
HTTPClient
从不关闭连接

有没有办法不等待回复?这肯定会给服务器增加很多开销,因为所有试图连接到它的Android应用程序都会保持连接的活力,并且可能会在应用程序本身上造成许多问题


基本上,我正在寻找的是一种方法,它允许我发送到
POST
消息,并在发送请求后立即终止连接,因为没有响应

只需使用HttpClient对象设置
ConnectionTimeOut
(代码供您理解,在您的情况下可能会有所不同)

现在,它将在您定义的TimeoOut之后终止连接。但请确保这将引发
TimeOutException
,因此您必须在HttpRequest中处理此异常。。(使用Try-catch)

编辑:或者您可以使用HttpRequestExecutor

从包装类别

确定响应是否带有实体。此类中的实现基于RFC2616。未知方法和响应代码被认为是用实体来表示响应。
派生执行器可以重写此方法来处理RFC 2616中未指定的方法和响应代码。

这真的没有意义。为什么没有回应?除非您进行长时间的轮询,否则应该会有一些响应,即使它没有主体,只有状态码。如果您没有得到/等待响应,如何确保服务器收到您的请求?
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");
protected boolean canResponseHaveBody (HttpRequest request, HttpResponse response)