很久以来我一直坚持这个问题,从我的androidApp HTTP习惯了我的Cable Modem HTTP服务器,但在客户端使用 linux mini_httpd 我
后很快得到这个org.apache.http.client.ClientProtocolExceptionhttpClient.execute(httpget);
我在发布此问题之前做了很多研究,之前我没有将contentType设置为xml,但我正在这样做
httpget.setHeader("Content-Type", "application/xml"); but still struggling with same error.
详细代码如下
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 25000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpGet httpget = new HttpGet(URL);
httpget.setHeader("Content-Type", "application/xml");
httpget.addHeader("Accept", "text/xml");
httpget.setParams(httpParameters);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
try {
response = httpClient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
Log.d("Http response", "response string got");
/* parse the response received from server */
try {
cmInfo = xmlParser.parse(response.getEntity().getContent());
Log.d("Http Response","back in main");
} catch (XmlPullParserException e1) {
Log.d("parse:", "XmlPullParserException");
e1.printStackTrace();
} catch (IOException e1) {
Log.d("parse:", "IOException");
e1.printStackTrace();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), errMsg2, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), errMsg1, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Connection Refused from server or Connection Timeout", Toast.LENGTH_LONG).show();
}
请建议做什么或有任何解决方法。
这是logcat
03-18 18:41:08.246: D/getWifiDetails: cmURL:(32090): http://192.168.0.1:50010?
03-18 18:41:08.246: D/WizardYourWifiActivity: URL-(32090): http://192.168.0.1:50010?get_data=PARM_WIFI_SSIDNAME_2_4G,PARM_WIFI_SSIDNAME_5G,PARM_WIFI_SECURITYMODE_2_4G,PARM_WIFI_SECURITYMODE_5G,PARM_WIFI_PASSPHRASE_2_4G,PARM_WIFI_PASSPHRASE_5G,PARM_WIFI_MODE_2_4G,PARM_WIFI_MODE_5G,PARM_WIFI_POWER_2_4G,PARM_WIFI_POWER_5G,PARM_WIFI_CHANNEL_2_4G,PARM_WIFI_CHANNEL_5G,PARM_WIFI_GUEST_NW_EN_2_4G,PARM_WIFI_GUEST_NW_EN_5G
03-18 18:41:08.707: W/System.err(32090): org.apache.http.client.ClientProtocolException
03-18 18:41:08.713: W/System.err(32090): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
03-18 18:41:08.713: W/System.err(32090): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-18 18:41:08.713: W/System.err(32090): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-18 18:41:08.713: W/System.err(32090): at com.arrisi.selfinstall.wizard.WizardYourWifiActivity.doCustomCreate(WizardYourWifiActivity.java:98)
03-18 18:41:08.713: W/System.err(32090): at com.arrisi.selfinstall.wizard.BaseWizardActivity.onCreate(BaseWizardActivity.java:44)
03-18 18:41:08.713: W/System.err(32090): at android.app.Activity.performCreate(Activity.java:5933)
03-18 18:41:08.713: W/System.err(32090): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-18 18:41:08.713: W/System.err(32090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-18 18:41:08.713: W/System.err(32090): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-18 18:41:08.713: W/System.err(32090): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-18 18:41:08.714: W/System.err(32090): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-18 18:41:08.714: W/System.err(32090): at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 18:41:08.714: W/System.err(32090): at android.os.Looper.loop(Looper.java:135)
03-18 18:41:08.714: W/System.err(32090): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-18 18:41:08.714: W/System.err(32090): at java.lang.reflect.Method.invoke(Native Method)
03-18 18:41:08.714: W/System.err(32090): at java.lang.reflect.Method.invoke(Method.java:372)
03-18 18:41:08.714: W/System.err(32090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-18 18:41:08.714: W/System.err(32090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-18 18:41:08.714: W/System.err(32090): Caused by: org.apache.http.ProtocolException: Invalid header: <?xml version="1.0" encoding="UTF-8"?>
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.impl.io.AbstractMessageParser.parseHeaders(AbstractMessageParser.java:162)
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:178)
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
03-18 18:41:08.714: W/System.err(32090): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
03-18 18:41:08.715: W/System.err(32090): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-18 18:41:08.715: W/System.err(32090): ... 17 more
答案 0 :(得分:0)
怀疑这个问题是由于服务器响应不当造成的。基本上
httpget.setHeader("Content-Type", "application/xml");
实际上在我的http客户端创建问题的服务器没有设置。 我使用HttpURLConnection代替appache.http,正如其他stackOverFlow答案中所建议的那样,但实际上并没有解决问题,但它帮助我缩小了问题范围。
感谢@vzamanillo的帮助