2013-03-12 49 views
0

嘿,我正在使用GPS位置,所以我得到座標,現在我需要的是將這些座標傳遞給我的Web服務並將其存儲到我的數據庫。但我有麻煩以適當的方式存儲它,我認爲我得到的數據對於數據庫來說太大了,這就是爲什麼我要將座標轉換爲十進制格式。當我在代碼中放入DecimaFormat方法時,我遇到了一些錯誤,你能幫助我嗎?轉換爲十進制格式時出錯

這些都是錯誤的:

03-13 22:03:16.733: E/WindowManager(1018): Activity com.example.projectthesis.Mapping2 has leaked window [email protected] that was originally added here 
03-13 22:03:16.733: E/WindowManager(1018): android.view.WindowLeaked: Activity com.example.projectthesis.Mapping2 has leaked window [email protected] that was originally added here 
03-13 22:03:16.733: E/WindowManager(1018): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344) 
03-13 22:03:16.733: E/WindowManager(1018): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 
03-13 22:03:16.733: E/WindowManager(1018): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
03-13 22:03:16.733: E/WindowManager(1018): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
03-13 22:03:16.733: E/WindowManager(1018): at android.view.Window$LocalWindowManager.addView(Window.java:537) 
03-13 22:03:16.733: E/WindowManager(1018): at android.app.Dialog.show(Dialog.java:278) 
03-13 22:03:16.733: E/WindowManager(1018): at com.example.projectthesis.Mapping2$phpconnect.onPreExecute(Mapping2.java:190) 
03-13 22:03:16.733: E/WindowManager(1018): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561) 
03-13 22:03:16.733: E/WindowManager(1018): at android.os.AsyncTask.execute(AsyncTask.java:511) 
03-13 22:03:16.733: E/WindowManager(1018): at com.example.projectthesis.Mapping2$GPSLocationListener.onLocationChanged(Mapping2.java:124) 
03-13 22:03:16.733: E/WindowManager(1018): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:234) 
03-13 22:03:16.733: E/WindowManager(1018): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:167) 
03-13 22:03:16.733: E/WindowManager(1018): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:183) 
03-13 22:03:16.733: E/WindowManager(1018): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-13 22:03:16.733: E/WindowManager(1018): at android.os.Looper.loop(Looper.java:137) 
03-13 22:03:16.733: E/WindowManager(1018): at android.app.ActivityThread.main(ActivityThread.java:4424) 
03-13 22:03:16.733: E/WindowManager(1018): at java.lang.reflect.Method.invokeNative(Native Method) 
03-13 22:03:16.733: E/WindowManager(1018): at java.lang.reflect.Method.invoke(Method.java:511) 
03-13 22:03:16.733: E/WindowManager(1018): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
03-13 22:03:16.733: E/WindowManager(1018): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
03-13 22:03:16.733: E/WindowManager(1018): at dalvik.system.NativeStart.main(Native Method) 

,這些都是我的代碼(特別是在傳遞過程和DecimalFormat的方法):

class phpconnect extends AsyncTask<String, String, String> { 
     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(Mapping2.this); 
      pDialog.setMessage("Logging in.."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     @Override 
     protected String doInBackground(String... args) { 
      String strLatitude = Long.toString(ILatitude); 
      String strLongitude = Long.toString(ILongitude); 

      DecimalFormat df = new DecimalFormat("###.00000"); 
      String frmLat = df.format(strLatitude); 
      String frmLong = df.format(strLongitude); 
      // Building parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("latitude", frmLat)); 
      params.add(new BasicNameValuePair("longitude", frmLong)); 
      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
        "POST", params); 
      // check log cat fro response 
      Log.d("Create Response", json.toString()); 
      try { 
       int success = json.getInt(TAG_SUCCESS); 
       if (success == 1) { 
        // successfully updated 
        Intent i = new Intent(getApplicationContext(), 
          Mapping2.class); 

        startActivity(i); 
        // closing this screen 
        finish(); 
       } else { 
        // failed to create product 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    } 
+1

該錯誤與數據庫調用完全不相關。 – tadman 2013-03-12 20:28:51

+0

是的,我認爲這是因爲在十進制格式,我不知道如何處理它。在我放置代碼之前,它正在工作,但問題是我存儲在我的數據庫中的座標,它變成了999.99999 =,就像那樣。 – MakAdin 2013-03-12 20:34:28

回答

0

你應該傳遞一個longdoubleformat方法。當您通過String對象時,該方法會拋出IllegalArgumentException,導致您的doInBackground方法提前結束。因此,您在onPreExecute中創建的對話框不會被解僱並泄漏。

+0

好吧,我會嘗試。謝謝... – MakAdin 2013-03-13 00:42:44

1

我在你的代碼中發現奇怪的是,ILatitude和ILongitude顯然是Long類型。 緯度和經度最好以double的值表示。我認爲DecimalFormat在這裏並不是真的需要,而且一個簡單的String.valueOf(ILatitude);就足夠了,因爲你使得ILatitude成爲雙倍。

+0

是的,之前,它是雙格式,我只是試了很長時間,以便我可以看到這是否有效。謝謝你的回答。但你是否建議我使用String.valueOf(ILatitude); ? – MakAdin 2013-03-13 00:44:25

+0

嗯,是的,除非你需要以特定的方式格式化你的雙。例如用零填充或僅顯示三位小數。 – 2013-03-13 17:01:44