我有一个应用程序将一些数据提交到本地服务器,结果服务器将发回 JSON。说{状态:"成功"}
当我在 2.3 SDK 模拟器中运行该应用程序,但安装在 Galaxy Tab (2.2 SDK) 中时,它可以正常工作,相同的响应采用 html 的形式。
I/RESPONSE( 8190): <?xml version="1.0" encoding="utf-8"?>
I/RESPONSE( 8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
I/RESPONSE( 8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
I/RESPONSE( 8190): <html>
I/RESPONSE( 8190): <head>
I/RESPONSE( 8190): <title>417 Expectation Failed</title>
I/RESPONSE( 8190): </head>
I/RESPONSE( 8190): <body>
I/RESPONSE( 8190): <h1>Error 417 Expectation Failed</h1>
I/RESPONSE( 8190): <p>Expectation Failed</p>
I/RESPONSE( 8190): <h3>Guru Meditation:</h3>
I/RESPONSE( 8190): <p>XID: 1902486816</p>
I/RESPONSE( 8190): <hr>
I/RESPONSE( 8190): <address>
I/RESPONSE( 8190): <a href="http://www.varnish-cache.org/">Varnish cache server</a>
I/RESPONSE( 8190): </address>
I/RESPONSE( 8190): </body>
I/RESPONSE( 8190): </html>
I/RESPONSE( 8190): <--
I/RESPONSE( 8190):
I/RESPONSE( 8190): <?xml version="1.0" encoding="utf-8"?>
I/RESPONSE( 8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
I/RESPONSE( 8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
I/RESPONSE( 8190): <html>
I/RESPONSE( 8190): <head>
I/RESPONSE( 8190): <title>417 Expectation Failed</title>
I/RESPONSE( 8190): </head>
I/RESPONSE( 8190): <body>
I/RESPONSE( 8190): <h1>Error 417 Expectation Failed</h1>
I/RESPONSE( 8190): <p>Expectation Failed</p>
I/RESPONSE( 8190): <h3>Guru Meditation:</h3>
I/RESPONSE( 8190): <p>XID: 1902486816</p>
I/RESPONSE( 8190): <hr>
I/RESPONSE( 8190): <address>
I/RESPONSE( 8190): <a href="http://www.varnish-cache.org/">Varnish cache server</a>
I/RESPONSE( 8190): </address>
I/RESPONSE( 8190): </body>
I/RESPONSE( 8190): </html>
W/System.err( 8190): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of
W/System.err( 8190): <?xml version="1.0" encoding="utf-8"?>
W/System.err( 8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
W/System.err( 8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
W/System.err( 8190): <html>
W/System.err( 8190): <head>
W/System.err( 8190): <title>417 Expectation Failed</title>
W/System.err( 8190): </head>
W/System.err( 8190): <body>
W/System.err( 8190): <h1>Error 417 Expectation Failed</h1>
W/System.err( 8190): <p>Expectation Failed</p>
W/System.err( 8190): <h3>Guru Meditation:</h3>
W/System.err( 8190): <p>XID: 1902486816</p>
W/System.err( 8190): <hr>
W/System.err( 8190): <address>
W/System.err( 8190): <a href="http://www.varnish-cache.org/">Varnish cache server</a>
W/System.err( 8190): </address>
W/System.err( 8190): </body>
W/System.err( 8190): </html>
Edit:请求发送:-
try {
HttpClient client = new DefaultHttpClient();
String postURL = GlobalCodes.getBaseurl();
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("show",
"testpost"));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
String str2 = EntityUtils.toString(resEntity);
Log.i("RESPONSE", " <--");
if (resEntity != null) {
Log.i("RESPONSE","**");
JSONObject jsonObj = new JSONObject(str2);
if (jsonObj.getString("status").equalsIgnoreCase("succuss")) {
.....
} else {
......
}
}
} catch (Exception e) {
e.printStackTrace();
}
谁能告诉我出了什么问题吗?
快乐编码..!
Galaxy Tab 必须向模拟器发送不同的标头。尝试禁用 HttpPost 对象上的期望标头。
httppost.getParams().setBooleanParameter( "http.protocol.expect-continue", false )
另请参阅照片上传期间 Android 的预期失败 https://stackoverflow.com/questions/5017751/expectation-failed-in-android-during-photo-upload
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)