How can I send the GPS and Network location cordinates to a server (static IP)?

后端 未结 2 1515
萌比男神i
萌比男神i 2021-01-07 04:51

I am a beginner in the Android Development.I want to make an android app which sends the GPS and network locations (lat & long) to my server (static IP). I have found th

2条回答
  •  感情败类
    2021-01-07 05:36

    Your problem is that

     public void onClick(View v) {
                postData(lat, lon);
            }
    

    runs on the UI thread. It means that while you do the "post" action, the UI isn't refreshed and cannot be accessed - your app get stuck until the post ends.

    You need to do something like:

      public void onClick(View v) {
            PostAsyncTask postAsyncTask = new PostAsyncTask();
            postAsyncTask.execute();
        }
    
    
    
     private class PostAsyncTask extends AsyncTask {
     protected Long doInBackground(URL... urls) {
         postData(lat, lon);
         return 0;
     }
    
     protected void onPreExecute() {
         // show progress dialog
     }
    
    
         protected void onPostExecute(Long result) {
             // hide  progress dialog
         }
     }
    

提交回复
热议问题