ppamorim

Untitled

Sep 26th, 2014
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import android.content.Context;
  2. import android.os.AsyncTask;
  3. import android.os.Build;
  4.  
  5. import com.Tag_Interativa.vejo_ao_vivo.app.core.service.AbstractService;
  6. import com.squareup.okhttp.OkHttpClient;
  7. import com.squareup.okhttp.Request;
  8. import com.squareup.okhttp.Response;
  9.  
  10. public class ScoreService extends AbstractService {
  11.  
  12.     private int mParam1;
  13.     private int mParam2;
  14.  
  15.     public ScoreService(int param1, int param2, OnRestResponse response) {
  16.         super(response);
  17.         mParam1 = param1;
  18.         mParam2 = param2;
  19.     }
  20.  
  21.     public void getScore() {
  22.         //GetScoreAsyncTaskParams não é necessário, é só um exemplo
  23.         GetScoreAsyncTaskParams params = new GetScoreAsyncTaskParams(mParam1, mParam2);
  24.         GetScoreAsyncTask getCommentsAsyncTask = new GetScoreAsyncTask();
  25.         if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
  26.             getCommentsAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
  27.         }
  28.         else {
  29.             getCommentsAsyncTask.execute(params);
  30.         }
  31.     }
  32.  
  33.     private static class GetScoreAsyncTaskParams {
  34.  
  35.         int mCamera;
  36.         int mPage;
  37.  
  38.         GetScoreAsyncTaskParams(int camera, int page) {
  39.             this.mCamera = camera;
  40.             this.mPage = page;
  41.         }
  42.     }
  43.  
  44.     private class GetScoreAsyncTask extends AsyncTask<GetScoreAsyncTaskParams, Void, String> {
  45.  
  46.         @Override
  47.         protected String doInBackground(GetScoreAsyncTaskParams... params) {
  48.  
  49.             try {
  50.  
  51.                 Request request = new Request.Builder()
  52.                         .url("http://192.241.176.128/facape/aluno.json")
  53.                         .build();
  54.  
  55.                 Response response = new OkHttpClient().newCall(request).execute();
  56.  
  57.                 if(response != null) {
  58.                     if (response.isSuccessful()) {
  59.                         return response.body().string();
  60.                     } else {
  61.                         return null;
  62.                     }
  63.                 } else {
  64.                     return null;
  65.                 }
  66.  
  67.             } catch (Exception e) {
  68.                 e.printStackTrace();
  69.                 return null;
  70.             }
  71.         }
  72.  
  73.         @Override
  74.         protected void onPostExecute( String response) {
  75.             super.onPostExecute(response);
  76.             if(response != null) {
  77.                 mRestResponse.onSuccess(AbstractService.SUCCESS, response);
  78.             } else {
  79.                 mRestResponse.onFail(AbstractService.ERROR);
  80.             }
  81.         }
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment