[英]Android from (HTTP) POST to GET
I am working on 2 the same apps that need to send to an php file the same string. 我正在2需要将相同的字符串发送到php文件的相同的应用程序。 The iOs one is a GET when i use this code in PHP to check it. 当我在PHP中使用此代码进行检查时,iOs就是GET。
file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));
But the Android one is a POST, but they need to be exactely the same, because i already build an working part of PHP that i cant change anymore. 但是Android是POST,但是它们必须完全相同,因为我已经构建了PHP的工作部分,现在无法更改了。
Here's my android code: 这是我的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();
}
Change: 更改:
HttpPost httpost = new HttpPost("http://Myserver.com/default.php");
To 至
HttpGet httpget = new HttpGet("http://Myserver.com/default.php");
In case you're getting the NetworkOnMainThreadException , it's because you're trying to do network operations from the main thread. 如果您遇到NetworkOnMainThreadException ,那是因为您试图从主线程进行网络操作。 If so, try looking into AsyncTask . 如果是这样,请尝试查看AsyncTask 。
Also make sure that you got internet permission in the android manifest. 还要确保您在android清单中获得了互联网许可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.