Android的Http请求ClientProtocolException [英] Android Http request ClientProtocolException

查看:208
本文介绍了Android的Http请求ClientProtocolException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个似乎是工作完美的,当我使用Firefox的REST客户端或谷歌Chrome浏览器的简单的REST客户端测试它的API
然而,当我使用Android SDK中发送HTTP请求我得到以下异常:

I have an API that seems to be working perfect when I test it using Firefox's "REST Client" or Google Chrome's "Simple REST Client" However, when I send an Http request using Android SDK I get the following exception:

ClientProtocolException:服务器没有一个有效的HTTP响应响应

ClientProtocolException: the server failed to respond with a valid HTTP response

还当我测试使用谷歌Chrome浏览器的高级REST客户端我得到了一个空的响应,所以什么可能会导致此任何想法?

also when I test using Google Chrome's "Advanced REST client" I got an empty response, so any idea about what might be causing this ?


DefaultHttpClient的HttpClient =新DefaultHttpClient();结果
HTT presponse HTT presponse = NULL;结果
HttpPost httpPost =新HttpPost(Constants.URL);结果
httpPost.setHeader(内容类型,应用/ JSON);结果
httpPost.setHeader(接受,应用/ JSON);结果
StringEntity SE =新StringEntity(参数,可以HTTP.UTF_8);结果
httpPost.setEntity(SE);结果
httpClient.execute(httpPost);

推荐答案

这应该工作

HttpParams httpParams=new BasicHttpParams();
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(Constants.URL);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
StringEntity se = new StringEntity(params,HTTP.UTF_8);
httpPost.setEntity(se);
httpClient.execute(httpPost);

这篇关于Android的Http请求ClientProtocolException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆