ppamorim

Untitled

Nov 11th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package m.m.com.m.core.service;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Build;
  5.  
  6. import com.squareup.okhttp.OkHttpClient;
  7. import com.squareup.okhttp.Request;
  8.  
  9. import java.io.IOException;
  10.  
  11. public class AbstractService extends AsyncTask<Request, Void, String> {
  12.  
  13.     private static final int ERROR_REQUEST = -1;
  14.  
  15.     private OkHttpClient client = new OkHttpClient();
  16.     private OnRequestResponse mOnRequestResponse;
  17.  
  18.     public AbstractService(Request request, OnRequestResponse onRequestResponse) {
  19.         mOnRequestResponse = onRequestResponse;
  20.  
  21.         if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
  22.             this.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, request);
  23.         } else {
  24.             this.execute(request);
  25.         }
  26.     }
  27.  
  28.     @Override
  29.     protected String doInBackground(Request... request) {
  30.     //Aqui ta dando a treta
  31.         try {
  32.             return client.newCall(request[0]).execute().body().string();
  33.         } catch (IOException e) {
  34.             e.printStackTrace();
  35.             return null;
  36.         }
  37.     }
  38.  
  39.     @Override
  40.     protected void onPostExecute(String result) {
  41.         super.onPostExecute(result);
  42.         if(result != null) {
  43.             mOnRequestResponse.onSucess(result);
  44.         } else {
  45.             mOnRequestResponse.onFail(ERROR_REQUEST);
  46.         }
  47.  
  48.     }
  49.  
  50.     public static interface OnRequestResponse {
  51.         public void onSucess(String response);
  52.         public void onFail(int error);
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment