我是初学者开发者,我正在制作一个Android GPS跟踪应用程序,我用它来收集lat和long,我也可以将电话号码和IMEI带到手机屏幕,我的问题是如何我可以将这些数据发送到我需要使用的服务器,这样我就可以在Google地图上跟踪用户,这是我的第一个问题&我希望我能够清楚地了解我的需求,谢谢你。
(这是我下面的代码,我添加一个帖子数据试试它不起作用)
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;
}
}
答案 0 :(得分:1)
您正在onCreate中调用postData();
。你现在还没有收到任何职位。
您必须将postData();
移至实际收到位置时,即在onLocationChanged中。
您可能还需要将其放在AsyncTask中。
答案 1 :(得分:0)
最近很多人都在问类似android app to send data to a server
的问题。虽然这是一个应该谷歌的基本编程问题,但我会尝试为开发人员提供一些指导谷歌。
选项1
开发J2EE Web服务,并将其托管在服务器上。您的Android应用需要将数据与Web服务相关联并将其发送到服务器。服务器可以接收数据并执行它想要的操作。服务器还可以通过Web服务的相同响应处理和响应更多信息。你需要谷歌hello world j2ee
。我找到了one。
一旦您的J2EE在本地桌面(或开发机器)上工作,您就可以将WAR或EAR复制到EC2,以使应用程序可以在互联网上运行,而无需经历创建网络的痛苦。打开和DMZ。
选项2
你可以开发一个基于java套接字的产品,你的android应用程序连接到,通过clientsocket。这样,您可以序列化数据并通过流发送数据。并在服务器端反序列化它并重建对象。这种方法需要更严格的软件开发。如果你是第一次开发人员,那可能是非常错误的,我会推荐选项1。
希望我已经给出了一些基本方向。