Android HttpPost-ClientProtocolException

Android HttpPost-ClientProtocolException,android,http,Android,Http,当我将http post JSON数据发布到服务器时,得到一个异常ClientProtocolException。有人能告诉我这段代码有什么问题吗 Logcat 06-11 10:30:56.062: W/System.err(435): org.apache.http.client.ClientProtocolException 06-11 10:30:56.122: W/System.err(435): at org.apache.http.impl.client.AbstractHttp

当我将http post JSON数据发布到服务器时,得到一个异常
ClientProtocolException
。有人能告诉我这段代码有什么问题吗

Logcat

06-11 10:30:56.062: W/System.err(435): org.apache.http.client.ClientProtocolException
06-11 10:30:56.122: W/System.err(435):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
06-11 10:30:56.182: W/System.err(435):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
06-11 10:30:56.212: W/System.err(435):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-11 10:30:56.212: W/System.err(435):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
06-11 10:30:56.212: W/System.err(435):  at com.vector.syncfunc.MainActivity.post(MainActivity.java:168)
代码:

这应该行得通

private String post(String url) {

String response = null;
try {

    boolean exception = false;
    StringBuilder builder = new StringBuilder();
    builder.append(getDeviceId());
    builder.append("|||");
    builder.append(getLastSyncDate());
    builder.append("|||");
    builder.append(getCurrentDateStr());
    String dateStr = builder.toString();
    HttpParams httpParams=new BasicHttpParams();
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
    HttpPost httpPost = new HttpPost(url);
                //httpPost.setHeader("Host", url);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");

    JSONObject jsonObject = new JSONObject();

    String param = null;
    try {
        param = encrypt(dateStr);
    } catch (InvalidKeyException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (NoSuchPaddingException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (IllegalBlockSizeException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (BadPaddingException e1) {
        e1.printStackTrace();
        exception = true;
    }

    if (exception) {
        return null;
    }

    try {
        jsonObject.put("encstring", param);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    httpPost.setHeader("Content-Length", ""+param.getBytes().length);
    StringEntity entity = new StringEntity(jsonObject.toString(),
            HTTP.UTF_8);
    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    httpPost.setEntity(entity);
    ResponseHandler responseHandler = new BasicResponseHandler();
    HttpResponse httpResponse = httpclient.execute(httpPost,
            responseHandler);
    StatusLine line = httpResponse.getStatusLine();
    if (line.getStatusCode() == 200) {
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();

        response = readString(inputStream);
    }

} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return response;
}
. 也许会有帮助。搜索这么多类似的帖子
private String post(String url) {

String response = null;
try {

    boolean exception = false;
    StringBuilder builder = new StringBuilder();
    builder.append(getDeviceId());
    builder.append("|||");
    builder.append(getLastSyncDate());
    builder.append("|||");
    builder.append(getCurrentDateStr());
    String dateStr = builder.toString();
    HttpParams httpParams=new BasicHttpParams();
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
    HttpPost httpPost = new HttpPost(url);
                //httpPost.setHeader("Host", url);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");

    JSONObject jsonObject = new JSONObject();

    String param = null;
    try {
        param = encrypt(dateStr);
    } catch (InvalidKeyException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (NoSuchPaddingException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (IllegalBlockSizeException e1) {
        e1.printStackTrace();
        exception = true;
    } catch (BadPaddingException e1) {
        e1.printStackTrace();
        exception = true;
    }

    if (exception) {
        return null;
    }

    try {
        jsonObject.put("encstring", param);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    httpPost.setHeader("Content-Length", ""+param.getBytes().length);
    StringEntity entity = new StringEntity(jsonObject.toString(),
            HTTP.UTF_8);
    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    httpPost.setEntity(entity);
    ResponseHandler responseHandler = new BasicResponseHandler();
    HttpResponse httpResponse = httpclient.execute(httpPost,
            responseHandler);
    StatusLine line = httpResponse.getStatusLine();
    if (line.getStatusCode() == 200) {
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();

        response = readString(inputStream);
    }

} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return response;
}