我是一名初学者开发人员,我正在制作一个 android GPS 跟踪应用程序,我用它来收集经纬度,我还可以将电话号码和 IMEI 带到电话屏幕上,我的问题是如何发送这些数据到我需要使用的服务器,这样我就可以在谷歌地图上追踪用户,这是我的第一个问题,我希望我足够清楚,让你了解我的需求,谢谢。
(这是我下面的代码,我添加了一个帖子数据尝试它不起作用)
package com.exemple.travelinsave;
@SuppressWarnings("unused")
public class PrincipalActivity extends Activity {
TextView Textlat; //= (TextView) findViewById(R.id.textlat);
TextView Textlong; //= (TextView) findViewById(R.id.textlong);
TextView EditNum; //= (TextView) findViewById(R.id.textlat);
TextView EditIMEI; //= (TextView) findViewById(R.id.textlong);
private final String TAG = "DemoButtonApp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
Textlat = (TextView)findViewById(R.id.Textlat);
Textlong = (TextView)findViewById(R.id.Textlong);
EditNum = (TextView)findViewById(R.id.EditNum);
EditIMEI = (TextView)findViewById(R.id.EditIMEI);
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String IMEINum = manager.getDeviceId();
String phonenum = manager.getLine1Number();
EditIMEI.setText(IMEINum);
EditNum.setText(phonenum);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener ();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
SetupsendButton();
}
class mylocationlistener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
if(location !=null)
{
double pLat = location.getLatitude();
double pLong = location.getLongitude();
Textlat.setText(Double.toString(pLat));
Textlong.setText(Double.toString(pLong));
}
}
}
private void SetupsendButton() {
// TODO Auto-generated method stub
Button SendButton = (Button) findViewById(R.id.sendButton);
SendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Log.i(TAG, "You clicked the bottuon!");
//Toast.makeText(PrincipalActivity.this, "You Clicked it ", Toast.LENGTH_LONG).show();
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://cybergracz.com/wp-includes/post.php";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>(4);
Textlat = (TextView)findViewById(R.id.Textlat);
Textlong = (TextView)findViewById(R.id.Textlong);
EditNum = (TextView)findViewById(R.id.EditNum);
EditIMEI = (TextView)findViewById(R.id.EditIMEI);
params.add(new BasicNameValuePair("PhoneNUM", String.valueOf(EditNum.getText())));
params.add(new BasicNameValuePair("Latitude ", String.valueOf(Textlat.getText())));
params.add(new BasicNameValuePair("Longitude", String.valueOf(Textlong.getText())));
params.add(new BasicNameValuePair("DeviceID", String.valueOf(EditIMEI.getText())));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
try {
HttpClient client = new DefaultHttpClient();
Textlat = (TextView)findViewById(R.id.Textlat);
Textlong = (TextView)findViewById(R.id.Textlong);
EditNum = (TextView)findViewById(R.id.EditNum);
EditIMEI = (TextView)findViewById(R.id.EditIMEI);
String PostDatS = "EditNum=" + EditNum;
PostDatS = PostDatS + "EditIMEI=" + EditIMEI;
PostDatS = PostDatS + "Textlat=" + Textlat;
PostDatS = PostDatS + "Textlong=" + Textlong;
String postURL = "http://starcitydirt.com/get.php?" + PostDatS;
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "kris"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.principal, menu);
return true;
}
}