【问题标题】:org.apache.http.client.ClientProtocolException in HttpPostHttpPost 中的 org.apache.http.client.ClientProtocolException
【发布时间】:2013-03-21 13:59:57
【问题描述】:

我尝试将一些字符串上传到服务器。当我尝试在服务器上上传时,在字符串中:

        HttpResponse response = httpclient.execute(httppost);

我有错误 org.apache.http.client.ClientProtocolException。所有代码:

public void sendString(String stringToSend) {

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        HttpPost httppost = new HttpPost(serverAddress);
        InputStreamEntity reqEntity = new InputStreamEntity( new ByteArrayInputStream(stringToSend.getBytes()), stringToSend.length());
        reqEntity.setContentType("application/xml");
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() != org.apache.http.HttpStatus.SC_OK) {
            Log.i("SEND", "not send "+response.getStatusLine());
        }else{
            Log.i("SEND", "send ok "+response.getStatusLine());
        }
    } catch (IOException e) {
        Log.w("IOException", e.toString() +" "+ e.getMessage());
    }        
}

【问题讨论】:

  • 抱歉,我找到了答案。如果有人会出现这个错误,我会这样修复它: StringEntity se = new StringEntity(stringToSend, HTTP.UTF_8); se.setContentType("文本/xml"); httppost.setEntity(se);

标签: java android http-post


【解决方案1】:

这应该可以工作

public void sendString(String stringToSend) {

try {
    HttpParams httpParams=new BasicHttpParams();
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    HttpPost httppost = new HttpPost(serverAddress);
    InputStreamEntity reqEntity = new InputStreamEntity( new ByteArrayInputStream(stringToSend.getBytes()), stringToSend.length());
    reqEntity.setContentType("application/xml");
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    if (response.getStatusLine().getStatusCode() != org.apache.http.HttpStatus.SC_OK) {
        Log.i("SEND", "not send "+response.getStatusLine());
    }else{
        Log.i("SEND", "send ok "+response.getStatusLine());
    }
} catch (IOException e) {
    Log.w("IOException", e.toString() +" "+ e.getMessage());
}        

}

【讨论】:

  • 就我而言,我有一些带有空白或空值的标题。删除它们解决了我的问题
猜你喜欢
  • 1970-01-01
  • 2015-02-19
  • 2023-04-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多