2012-06-14 194 views
0

我做了一個GPS程序。我想將當前地址發送到服務器。我陷入了字符串部分。你能糾正它嗎?我的代碼是:發送GPS數據到服務器

     try { 
     Geocoder mGC = new Geocoder(this, Locale.ENGLISH); 
     addresses = mGC.getFromLocation(mLocation.getLatitude(), 
       mLocation.getLongitude(), 1); 
     if (addresses != null) { 
      Address currentAddr = addresses.get(0); 
      StringBuilder mSB = new StringBuilder("Address:\n"); 
      for (int i = 0; i < currentAddr.getMaxAddressLineIndex(); i++) { 
       mSB.append(currentAddr.getAddressLine(i)).append("\n"); 
       String a=mSB.toString(); 
     if(a!=null){ 

         HttpClient httpclient = new DefaultHttpClient(); 
         HttpPost httppost = new HttpPost(
           "URL"); 

         try { 

          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
            1); 
          nameValuePairs.add(new BasicNameValuePair(
            "data",a)); 
          httppost.setEntity(new UrlEncodedFormEntity(
            nameValuePairs)); 
          HttpResponse response = httpclient 
            .execute(httppost); 
          Toast.makeText(getBaseContext(), 
            response.toString(), 
            Toast.LENGTH_LONG).show(); 
          continue; 

}

+0

*我得到卡住*??你得到什麼輸出/錯誤? – GAMA

+0

你爲什麼在循環中建立http連接? –

+0

要求將地址轉換爲字符串 – user1437027

回答

0

1不知道,但HTTP連接不應該是循環

2 - 創建的最高位,但不用於

StringBuilder mSB = new StringBuilder("Address:\n"); 
      for (int i = 0; i < currentAddr.getMaxAddressLineIndex(); i++) { 
       mSB.append(currentAddr.getAddressLine(i)).append("\n"); 
       } <----------------- loop should end here 
       String a= mSB.toString() ;<-------------------------- carete string from mSB 
+0

但爲什麼發送地址兩次? – user1437027

+0

沒有得到? ............................... –

+0

可能是http連接在循環........ –