0

当我将 http post JSON 数据发布到服务器时,出现异常ClientProtocolException 。谁能告诉我这段代码有什么问题?

日志猫

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();

        DefaultHttpClient httpclient = new DefaultHttpClient();
        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;
}
4

1 回答 1

0

这应该工作

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;
}
于 2014-01-07T19:35:21.780 回答