2013-06-03 99 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

相關問題