我正在处理2个需要发送到php文件相同字符串的相同应用程序。 iOs之一是一个GET,当我在PHP中使用这个代码来检查它。Android从(HTTP)POST到GET
file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));
但Android的一个是职位,但他们需要exactely一样的,因为我已经把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();
}
那么你为什么不尝试HttpGet在Android? – user370305
刚刚改变了吗?因为我已经尝试过,但它坠毁 –
看看http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android /和http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/用于HttpGet。如果它崩溃,那么你必须发生崩溃.. – user370305