Advertisement
Anwar907

Untitled

Aug 13th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. package com.example.anwar.myasynctaskloader;
  2.  
  3. import android.content.AsyncTaskLoader;
  4. import android.content.Context;
  5.  
  6. import com.loopj.android.http.AsyncHttpResponseHandler;
  7. import com.loopj.android.http.SyncHttpClient;
  8.  
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. import cz.msebera.android.httpclient.Header;
  15.  
  16. public class MyAsyncTaskLoader extends AsyncTaskLoader<ArrayList<MovieItems>>{
  17.  
  18.     private ArrayList<MovieItems> mData;
  19.     private boolean mHasResult =false;
  20.     private String mMovieSearch;
  21.  
  22.     public MyAsyncTaskLoader(final Context context,String movieSearch){
  23.         super(context);
  24.  
  25.         onContentChanged();
  26.         this.mMovieSearch = movieSearch;
  27.     }
  28.  
  29.     @Override
  30.     protected void onStartLoading() {
  31.         if (takeContentChanged())
  32.             forceLoad();
  33.         else if (mHasResult)
  34.             deliverResult(mData);
  35.     }
  36.  
  37.     @Override
  38.     public void deliverResult(ArrayList<MovieItems> data) {
  39.         mData = data;
  40.         mHasResult = true;
  41.         super.deliverResult(data);
  42.     }
  43.  
  44.     @Override
  45.     protected void onReset() {
  46.         super.onReset();
  47.         onStopLoading();
  48.         if (mHasResult)
  49.             onReleaseResources(mData);
  50.         mData = null;
  51.         mHasResult = false;
  52.     }
  53.  
  54.     private static final String API_KEY = "6bb1e0745f7c266629f56a6705908e0a";
  55.  
  56.     @Override
  57.     public ArrayList<MovieItems> loadInBackground() {
  58.         SyncHttpClient client = new SyncHttpClient();
  59.  
  60.         final ArrayList<MovieItems> movieItemses = new ArrayList<>();
  61.         String url = "https://api.themoviedb.org/3/search/movie?api_key="
  62.                 +API_KEY+"&language=en-US&query="+mMovieSearch;
  63.  
  64.         client.get(url, new AsyncHttpResponseHandler() {
  65.  
  66.             @Override
  67.             public void onStart() {
  68.                 super.onStart();
  69.                 setUseSynchronousMode(true);
  70.             }
  71.  
  72.             @Override
  73.             public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
  74.                 try {
  75.                     String result = new String(responseBody);
  76.                     JSONObject responseObject = new JSONObject(result);
  77.                     JSONArray list = responseObject.getJSONArray("results");
  78.                     for (int i=0;i<list.length();i++){
  79.                         JSONObject moviedetail = list.getJSONObject(i);
  80.                         MovieItems movieItems = new MovieItems(moviedetail);
  81.                         movieItemses.add(movieItems);
  82.                     }
  83.                 }catch (Exception e){
  84.                     e.printStackTrace();
  85.                 }
  86.             }
  87.  
  88.             @Override
  89.             public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
  90.  
  91.             }
  92.         });
  93.         return movieItemses;}
  94.  
  95.     private void onReleaseResources(ArrayList<MovieItems> data){
  96.  
  97.     }
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement