Php Android从(HTTP)POST获取
我正在开发两个相同的应用程序,它们需要向php文件发送相同的字符串。当我在PHP中使用此代码进行检查时,iOs one是一个GETPhp Android从(HTTP)POST获取,php,android,http-post,http-get,Php,Android,Http Post,Http Get,我正在开发两个相同的应用程序,它们需要向php文件发送相同的字符串。当我在PHP中使用此代码进行检查时,iOs one是一个GET file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true)); 但是Android版是一篇文章,但是它们必须完全相同,因为我已经构建了PHP的一个工作部分,我不能再修改了 以下是我的android代码: Http
file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));
但是Android版是一篇文章,但是它们必须完全相同,因为我已经构建了PHP的一个工作部分,我不能再修改了
以下是我的android代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://Myserver.com/default.php");
json = new JSONObject();
try {
json.put("id", "69403");
json.put("longitude", longi);
json.put("latitude", lat);
json.put("timestamp", time);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/// create StringEntity with current json obejct
try {
// StringEntity se = new StringEntity(json.toString());
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data", json.toString()));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
System.out.println("send about to do post");
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("send post done");
HttpEntity entity = response.getEntity();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
更改:
HttpPost httpost = new HttpPost("http://Myserver.com/default.php");
到
如果你得到了,那是因为你试图从主线程进行网络操作。如果是这样,试着调查一下
还要确保您在android清单中获得了internet权限。那么您为什么不在android中尝试HttpGet?改变这一点就足够了吗?因为我已经试过了,但是它在HttpGet上崩溃了。如果它崩溃了,那么你必须在崩溃时发布..当我更改这个时,我得到一个错误:httpost.setEntitynew UrlEncodedFormEntitynvps,HTTP.UTF_8;它希望将其更改为HttpResponse httpost.setEntitynew UrlEncodedFormEntitynvps,HTTP.utf8;但是,你可以说得更清楚一些,因为你看到的错误消息是什么?setEntity上的错误。可能的解决方案:向httppost添加cast。然后发送到HttpResponse httpost.setEntitynew UrlEncodedFormEntitynvps,HTTP.UTF_8;这是因为HttpGet没有setEntity方法。你不能简单地用HttpGet替换HttpPost并期望一切正常。据我所知,您不能使用get发送请求正文中的任何内容。
HttpGet httpget = new HttpGet("http://Myserver.com/default.php");