HTTP body post in android

本秂侑毒 提交于 2019-12-13 07:25:43

问题


I am new to android development. In my application I have to use HTTP body post. But I'm getting an error.

Here is my code:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://67.64.238.61:8182/websearch");

try {
    StringEntity se = new StringEntity( "<Search><Login><User>test</User><Password>test</Password></Login><SearchWord>1234567890</SearchWord><NextToken></NextToken></Search>", HTTP.UTF_8);
    se.setContentType("text/xml");
    httppost.setEntity(se);

    //HttpResponse httpresponse = httpclient.execute(httppost);
    BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient .execute(httppost);
    HttpEntity resEntity = httpResponse.getEntity();
    String str = EntityUtils.toString(resEntity);
    System.out.println("==="+str);
    //tvData.setText(EntityUtils.toString(resEntity));

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I am getting this kind of error:

06-08 16:25:38.665: WARN/System.err(12122): org.apache.http.client.ClientProtocolException
06-08 16:25:38.665: WARN/System.err(12122):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
06-08 16:25:38.665: WARN/System.err(12122):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-08 16:25:38.675: WARN/System.err(12122):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-08 16:25:38.675: WARN/System.err(12122):     at com.texas.book.Search.search(Search.java:225)
06-08 16:25:38.675: WARN/System.err(12122):     at com.texas.book.Search$Progress.doInBackground(Search.java:196)
06-08 16:25:38.675: WARN/System.err(12122):     at com.texas.book.Search$Progress.doInBackground(Search.java:1)
06-08 16:25:38.675: WARN/System.err(12122):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-08 16:25:38.675: WARN/System.err(12122):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-08 16:25:38.675: WARN/System.err(12122):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-08 16:25:38.675: WARN/System.err(12122):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-08 16:25:38.685: WARN/System.err(12122):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-08 16:25:38.685: WARN/System.err(12122):     at java.lang.Thread.run(Thread.java:1096)
06-08 16:25:38.685: WARN/System.err(12122): Caused by: org.apache.http.ProtocolException: Unable to parse status code from status line: HTTP/1.1 ñðð@ã¤
06-08 16:25:38.685: WARN/System.err(12122):     at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:176)
06-08 16:25:38.685: WARN/System.err(12122):     at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
06-08 16:25:38.685: WARN/System.err(12122):     at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
06-08 16:25:38.695: WARN/System.err(12122):     at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
06-08 16:25:38.695: WARN/System.err(12122):     at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:219)
06-08 16:25:38.695: WARN/System.err(12122):     at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:119)
06-08 16:25:38.695: WARN/System.err(12122):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
06-08 16:25:38.695: WARN/System.err(12122):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-08 16:25:38.695: WARN/System.err(12122): Caused by: org.apache.http.ParseException: Unable to parse status code from status line: HTTP/1.1 ñðð@ã¤
06-08 16:25:38.715: WARN/System.err(12122):     at org.apache.http.message.BasicLineParser.parseStatusLine(BasicLineParser.java:424)
06-08 16:25:38.715: WARN/System.err(12122):     at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:99)
06-08 16:25:38.715: WARN/System.err(12122):     at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
06-08 16:25:38.715: WARN/System.err(12122):     ... 18 more

回答1:


The server returned a response that you can't handle. Well, you do handle it as the response goes to ClientProtocolException

Most likely you need to be setting some header information your HttpPost object with the setHeader function.

This is an issue with the server you are trying to connect to simply not accepting what you sent it.



来源:https://stackoverflow.com/questions/10956351/http-body-post-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!