Java Android HTTP请求问题
我试图从我在Andorid Studio中构建的应用程序中触发http请求,但看到一些bizzare行为 以下代码在运行时返回HTTP/1.1200,但php脚本不运行。为了检查php脚本是否正确,我将其复制并粘贴到浏览器中,它运行得非常好Java Android HTTP请求问题,java,android,Java,Android,我试图从我在Andorid Studio中构建的应用程序中触发http请求,但看到一些bizzare行为 以下代码在运行时返回HTTP/1.1200,但php脚本不运行。为了检查php脚本是否正确,我将其复制并粘贴到浏览器中,它运行得非常好 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.domain.com/script.php");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.domain.com/script.php");
try {
// Add your data
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
mLastUpdateTimeTextView.setText(response.getStatusLine().toString());
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请任何人告诉我为什么我会得到200 ok的回复,但脚本没有运行
只需添加,我已在清单文件中设置了Internet权限
以下是日志:
02-27 16:33:13.030 26409-26675/?I/ExchangeSyncSources﹕ [NA
][HttpClientLifeCycle]已创建:1107382416 02-27 16:33:14.370
26528-26528/? I/GHttpClientFactory﹕ 使用GMSCore的
GoogleHttpClient 02-27 16:33:14.590 9554-26702/?
I/谷歌电子工厂﹕ 绑定HttpService 02-27 16:33:14.600
9554-26702/? I/GoogleHttpClient﹕ 回到过去
SSLCertificateSocketFactory 02-27 16:33:14.820 26409-26675/?
I/ExchangeSyncSources﹕ [NA][HttpClientLifeCycle]已关闭:1107382416
02-27 16:33:14.900 26409-26675/? I/BaseSyncSource﹕ [NA
][HttpClientLifeCycle,3]已创建:1108133120 02-27 16:33:15.920
26409-26675/? I/BaseSyncSource﹕ [NA][HttpClientLifeCycle,3]已关闭:
1108133120 02-27 16:33:16.030 26409-26675/? I/BaseSyncSource﹕ [NA
][HttpClientLifeCycle,3]已创建:1107653144 02-27 16:33:17.890
26409-26675/? I/BaseSyncSource﹕ [NA][HttpClientLifeCycle,3]已关闭:
1107653144 02-27 16:33:18.160 26409-26751/? I/BaseSyncSource﹕ [NA
][HttpClientLifeCycle,1]已创建:1106603920 02-27 16:33:18.970
26342-26342/com.example.matt.myapplication D/Http Post响应:﹕
org.apache.http.message。BasicHttpResponse@4201f0e8 02-27 16:33:19.050
26409-26751/? I/BaseSyncSource﹕ [NA][HttpClientLifeCycle,1]已关闭:
1106603920 02-27 16:33:19.180 26409-26757/? I/BaseSyncSource﹕ [NA
][HttpClientLifeCycle,2]已创建:1106203584 02-27 16:33:19.930
26409-26757/? I/BaseSyncSource﹕ [NA][HttpClientLifeCycle,2]已关闭:
1106203584 02-27 16:33:20.150 26409-26773/? I/BaseSyncSource﹕ [NA
][HttpClientLifeCycle,4]已创建:1108392040 02-27 16:33:21.040
26409-26773/? I/BaseSyncSource﹕ [NA][HttpClientLifeCycle,4]已关闭:
1108392040
我有一些类似的方法写了一段时间前,有一些小的差异。据我所知,“HttpClient”和“DefaultHttpClient”之间存在差异。您可以这样尝试:
DefaultHttpClient locationClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.domain.com/script.php");
HttpResponse response = null;
try {
response = locationClient.execute(httpPost);
}
catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
}
catch (IOException e) {
Log.e(TAG, e.toString());
}
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// do something
}
else {
Log.e(TAG, "Empty response");
}
所以我不得不承认,我已经找到了解决方案,这与url大小写有关 url中有一个大写字母,在我服务器上的文件名中没有。这导致HTTP请求运行404错误页面 该链接在浏览器中起作用,但在Java中不是HTTP请求中的url路径
现在,我知道这可能被视为无carless,但我想指出的是,在通过Android发出HTTP请求时,这可能是一个潜在的问题返回了什么?200不意味着httpPost请求成功,所以我想脚本已经执行了。但要了解更多信息,我们需要更多细节;用logcat编辑问题你确定你想要帖子而不是GET吗?这似乎也不起作用,因为当我开始在我的新手机上调试时,它才刚刚开始发生