1

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

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

1 回答 1

0

这应该工作

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

}

于 2014-01-07T19:38:25.807 回答