Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpPost中的org.apache.http.client.ClientProtocolException_Java_Android_Http Post - Fatal编程技术网

Java HttpPost中的org.apache.http.client.ClientProtocolException

Java HttpPost中的org.apache.http.client.ClientProtocolException,java,android,http-post,Java,Android,Http Post,我尝试上传一些字符串到服务器。当我尝试在服务器上上载时,以字符串形式: HttpResponse response = httpclient.execute(httppost); 我有错误org.apache.http.client.ClientProtocolException。所有代码: public void sendString(String stringToSend) { try { DefaultHttpClient httpclient

我尝试上传一些字符串到服务器。当我尝试在服务器上上载时,以字符串形式:

        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());
    }        
}
这应该行得通

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

}

很抱歉,我找到了答案。如果有人会出现此错误,我会这样修复:StringEntity se=new StringEntity stringToSend,HTTP.UTF_8;se.setContentTypetext/xml;httppost.setEntityse;在我的例子中,我有一些带有空白或空值的标题。移除它们解决了我的问题