2013-06-03 100 views
0

我是一个初学者开发者,我正在做一个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; 
    } 


} 
+0

你应该只在一个帖子中提出一个问题。这里有两个完全独立的问题。 – AlexWien

+0

那么任何人都可以帮忙? – WadiSoft

+0

什么不起作用? – FWeigl

回答

1

要调用中的onCreate postData();。您目前还没有收到任何表态。

你必须移动到postData();当你真正在onLocationChanged收到一个位置,即。

你也可能需要把它放在一个AsyncTask中。

+0

你会检查源代码现在,我添加一个按钮来看看它是否会工作,但它不同我,关于AsyncTask我仍然使用它来了解更多,请原谅我的无知谢谢! – WadiSoft

0

最近很多人都在问,类似于android app to send data to a server问题。虽然这是一个基本的编程问题,应该google'd,我会尽力为开发人员提供一些方向谷歌。

选项1

开发一个Web服务J2EE,并将其驻留在服务器上。您的Android应用程序需要将数据与Web服务关联并将其发送到服务器。服务器可以接收数据并按照它的要求进行操作。服务器还可以通过web服务的相同响应来处理和响应更多信息。你需要谷歌hello world j2ee。我发现one

一旦您的J2EE在本地桌面(或开发机器)上工作,您可以将WAR或EAR复制到EC2,以使应用程序可以通过互联网工作,而不必经历创建网络的痛苦是开放的和一个DMZ。

选项2

你可以开发一个基于java的插座产品,你的Android应用程序连接,直通ClientSocket的。这样你可以序列化数据并通过流发送。并在服务器端反序列化并重建对象。这种方法需要更有纪律的软件开发。它可以是非常错误,如果你是第一次开发者,我建议选择1

希望我已经给出了一些基本的方向。

+0

应用程序正在收集纬度,经度和设备信息,我需要一种方法来通过HTTP请求数据发送到我的PHP文件:邮寄或的GetData这样File.php INFO1 =值&INFO2 =数值,并感谢您的时间 – WadiSoft

+0

HTTP ://website.com/LogGetDt。txt http://website.com/get.php?deviceid=31231313131313213132&devicenum=212625124423&lant=14.215465464&long=5.254654654654 – WadiSoft

相关问题