Skip to content

Instantly share code, notes, and snippets.

@lalitsonawane
Created June 4, 2017 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lalitsonawane/47ad8aeb3d25a93abeb40b25bfb6665b to your computer and use it in GitHub Desktop.
Save lalitsonawane/47ad8aeb3d25a93abeb40b25bfb6665b to your computer and use it in GitHub Desktop.
Showing error
package in.apptonic.lalit.cardviewsample;
import android.content.Context;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import in.apptonic.lalit.cardviewsample.model.News;
/**
* Created by lalitkumarsonawane on 03/06/17.
*/
public class BackgroundTask {
Context context;
ArrayList<News> arrayList = new ArrayList<>();
String url = "https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=eff7c7c5b2b7470e9f785483a9d9520b";
public BackgroundTask(Context context) {
this.context = context;
}
public ArrayList<News> getList() {
final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count = 0;
while (count < response.length()) {
try {
JSONObject jsonObject = response.getJSONObject(count);
News news = new News(jsonObject.getString("title"), jsonObject.getString("description"), jsonObject.getString("urlToImage"));
arrayList.add(news);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "Error while downloading news", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}
);
MySingleton.getmInstance(context).addToRequestque(jsonArrayRequest);
return arrayList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment