我正在尝试将GPS位置从设备发送到服务器。 但是我面临的问题是该位置没有发送到我的服务器。
我的代码看起来像这样;
//###################################################################################################################
public void postData(String la, String lo) { //#
HttpClient httpclient = new DefaultHttpClient(); //#
HttpGet htget = new HttpGet("http://My server IP/GPS/"+la+"/"+lo); //#
//#
try { /*##################################*/ //#
HttpResponse response = httpclient.execute(htget); /* send it to my server............ */ //#
String resp = response.getStatusLine().toString(); /*##################################*/ //#
Toast.makeText(this, resp, 5000).show(); //#
//#
} catch (ClientProtocolException e) { //#
Toast.makeText(this, "Error #1", 5000).show(); //#
} catch (IOException e) { //#
Toast.makeText(this, "Error #2", 5000).show(); //#
} //#
} //#
我没有使用端口号,因为该文件将靠近我的网络服务器文件。 (默认值为80作为端口号。) 我的网络服务器是可以访问的,所以我相信名为“GPS”的文件夹也必须是可以访问的。 我给了RWX许可。
我还在我的xml文件中添加了以下代码
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
但我跑的时候没有运气...... 有什么帮助吗?
服务器端的图片:
我获得代码的来源是:here
答案 0 :(得分:0)
//这对我来说很好....希望它也能帮到你
String url ="url of your web serive ";
Log.d("url to update location", url);
String response = HttpRequest.getDataFromServices(url);
try {
JSONObject jobj = new JSONObject(response);
if (jobj.getString("success").equalsIgnoreCase(
"true")) {
Log.d("updated", "Location udated");
} else {
Log.d("updated", "Location not udated");
Toast.makeText(Main.this,
"Unable to update Location",
Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}