Reputation: 143
private class GetRecipe extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Activity_Home_List.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray hits = jsonObj.getJSONArray("hits");
// looping through All recipe
for (int i = 0; i < hits.length(); i++) {
JSONObject h = hits.getJSONObject(i);
JSONObject recipe = h.getJSONObject("recipe");
String Uri = recipe.getString("uri");
String Label = recipe.getString("label");
String Image = recipe.getString("image");
// tmp hash map for single recipe
HashMap<String, String> recipe_ = new HashMap<>();
// adding each child node to HashMap key => value
recipe_.put("uri", Uri);
recipe_.put("label", Label);
recipe_.put("image", Image);
// adding recipe to recipe list
recipeList.add(recipe_);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Activity_Home_List.this, recipeList,
R.layout.home_list_items, new String[]{"uri", "label",
"image"}, new int[]{R.id.recipe,
R.id.label, R.id.image});
lv.setAdapter(adapter);
}
}
this is my handler class
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
I am getting string value but image is not coming in list item. can I get image as list item. this is my whole parsing code for my json but still image is not come in list item.
Upvotes: 1
Views: 710
Reputation: 10126
Try Glide.
1. Add Glide dependency to app/build.gradle
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
2. Load image using Glide
Glide.with(context).load(url).placeholder(R.drawable.placeholder).into(imageView);
OR
Glide.with (context).load (url).asBitmap().into(imageView);
Upvotes: 2
Reputation: 58
I don't know if it is correct in terms of "good programming practices", but you have to create a Fragment
from a Layout
, use another AsyncTask
to download it an then Inflate
it using the List
that you have.
Take a look here: it's great tutorial on YT
Upvotes: 2