【发布时间】:2014-08-06 16:43:01
【问题描述】:
我正在使用 GPS 我想在数据库中插入经度和纬度。在模拟器中它工作正常我将值插入数据库中,在移动设备中它不起作用,如何使用我的笔记本电脑作为服务器。我想插入经度和来自移动端的纬度正在使用网络服务(Json)。
public class AndroidGPSTrackingActivity extends Activity
{
public double latitude;
public double longitude;
Button btnShowLocation;
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
latitude = gps.getLatitude();
longitude = gps.getLongitude();
// \n is for new line
String Text = "My current Latitude = " + latitude + " Longitude = " + longitude;
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
SendQueryString();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
public void SendQueryString() {
new Thread() {
public void run() {
String url = "http://10.0.2.2/jobsonthego/jobs/gps.php?latitude=" + latitude +"&longitude=" + longitude;
//String url = "http://10.0.2.2/android/gps.php";
try {
HttpClient Client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
Client.execute(httpget);
}
catch(Exception ex) {
String fail = "Fail!";
Toast.makeText( getApplicationContext(),fail,Toast.LENGTH_SHORT).show();
}
}
}.start();
}
});
}
}
【问题讨论】:
-
可能是因为没有找到10.0.2.2?在模拟器中它是本地的,但在设备中它有点不同
-
您的笔记本电脑是否设置为接受 HTTP 请求?
-
对不起,我是 android 的初学者。如何在笔记本电脑中设置接受 HTTP 请求?