Skip to content Skip to sidebar Skip to footer

Reuse Httpurlconnection And Asyntask For Rest Requests

I have PostData class to transmit data lat, long,mac,.. to the server which is being sent to it from the service class. In PostData class, the data is being processed with the aid

Solution 1:

This may set you on a path of some code refactoring, but for general good practice regarding REST requests you should look into Volley, or Retrofit, (also another SO question regarding retrofit that might help).

these libraries are very efficient performance-wise, and in the long run will save you a lot of grief, they take care of the background threading, and you won't necessarily need to explicitly use HttpUrlConnection.

Hope this helps.

Edit :

To further answer your question - if you do wish to specifically use AsyncTask - you should use the PostData class as a general purpose class, in your case for the network operations (might also possibly want to make it a Singleton, and give it a more general name).

and yes your implementation looks like you should be able to use it, and any corrections\changes\additions should be made in the AsyncTask itself under PostData, no need for another general class, if need be - you can just add more inner AsyncTask subclasses.

my (very very general) direction would be:

publicclassNetworkData {
String jSONString;
privateAsyncTaskCallback callback;

publicNetworkData(AsyncTaskCallback callback) {
    this.callback = callback;
}

publicStringgetjSONString() {
    return jSONString;

}

publicvoidsetjSONString(String jSONString) {
    this.jSONString = jSONString;
}

//let's say this is for post requests...publicvoidpostData(String jSONString, Context context) {
    this.jSONString = jSONString;

    newMyPostTask(context).execute(jSONString);

}

//let's say this is for get requests...publicvoidgetData(String jSONString, Context context) {
    this.jSONString = jSONString;

    newMyGetTask(context).execute(jSONString);

}

classMyPostTaskextendsAsyncTask<String, Integer, ArrayList<Integer>> {
    final Context mContext;
    ArrayList<Integer> routes = newArrayList<Integer>();
    double distance;

    publicMyPostTask(Context context) {
        mContext = context;
    }

    @OverrideprotectedArrayList<Integer> doInBackground(String... params) 
    {
        try
        {
         //do you stuff for post requests...
        } catch (IOException e) 
        {
         //...
        } 
        finally 
            {
             //...
            }
        }
     }

classMyGetTaskextendsAsyncTask<String, Void, ArrayList<Object>> {
    final Context mContext;
    ArrayList<Object> routes = newArrayList<Object>();

    publicMyPostTask(Context context) {
        mContext = context;
    }

    @OverrideprotectedArrayList<Object> doInBackground(String... params) 
    {
        try
        {
         //do you stuff for get requests...
        } 
        catch (IOException e) 
        {
         //...
        } 
        finally 
        {
         //...
        }
      }
    }
}

If you do choose to use Volley or Retrofit, then keep using the general class structure and just modify it's utilities and replace the requset format (i.e. instead of the AsyncTask parts).

Post a Comment for "Reuse Httpurlconnection And Asyntask For Rest Requests"