0

I have JSON string created by Gson library and I want to send this string with HttpURLConnection API "GET" to the localhost server(wamp). I will run the app on my S4 device that is connected with my laptop vie the USB cabel.

I appreciate any help.

I have this JSON string:

{
   "latitude":80.86907321,
   "longitude":15.66542435,
   "formatted":"22.04.2015 11:11:00",
   "route":4
}

I have this method in the inner class "MyLocationListener":

private String convertToJSON(double pLong, double pLat, String formatted) {
    //envelop the data in JSON format.                  
    Data d = new Data(pLat, pLong, formatted,route_number);
    Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, new DataSerializer()).create();
    return gson.toJson(d);
}
2
  • What's your question exactly? Commented Apr 22, 2015 at 12:08
  • @Sidd: How can I send this JSON string to my localhost with HttpURLConnection api? I will test the app on my device. Commented Apr 22, 2015 at 12:14

1 Answer 1

1

First, you have to understand the AsyncTask.

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

Here a link to AndroidDevelopper AsyncTask page

Second, you should creating a class dedicate to DB operations. Personnaly, I prefer HttpClient from Apache, but if you prefer HttpURLConnection, check this page. Both HttpClient & HttpURLConnection are defined (and some examples)

And, why not using POST if you want sending JSON datas?

Last thing : if you are using wamp with your localhost on a device, your url should be : http://192.168.X.X, but on emulator http://127.0.0.1

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I will use POST. I want to send the data to the server after 60 seconds so this mean I can not use AsyncTask class?
No, you can, you just need to set a timer to execute the task after 60 secs. But not with locking Main UI Thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.