我是一個初學者開發者,我正在做一個android的GPS跟蹤應用程序,我讓它收集拉特和長,也可以把電話號碼和IMEI到電話屏幕,我的問題是怎麼可以我發送這些數據到一個服務器,我需要使用,所以我可以跟蹤谷歌地圖上的用戶,這是我的第一個問題&我希望我已經足夠明確,讓您瞭解我的需求,謝謝。如何將數據從Android應用程序發送到服務器日期?
(這是我下面的代碼,我添加了一個帖子數據嘗試它不工作)
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;
}
}
你應該只在一個帖子中提出一個問題。這裏有兩個完全獨立的問題。 – AlexWien
那麼任何人都可以幫忙? – WadiSoft
什麼不起作用? – FWeigl