I have a JSON file want to retrieve url from it and display it image view. I use JSON PARSER ASYNC TASK to do that after post execution i run a task to show image view that not working
My Java Code
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
// Getting JSON Array
// Storing JSON item in a Variable
Url = http://icons.iconarchive.com/icons/benjigarner/softdimension/48/
String icon = json.getString("icon"); //URL String
String ImageUrl = Url + Icon;
//Function to get image from URL - First method
Drawable MyImage = loadloadImageFromURL(ImageUrl);
//Function to get image from URL - Second method
Bitmap MyImage = getBitmapFromURL(ImageUrl);
//Third Method
new ImageDownload().execute(ImageUrl);
ImageView Image = (ImageView) rootView.findViewById(R.id.imgMainWeather); Image.setImageBitmap(MainActivity.WeatherImage);
} catch (JSONException e) {
e.printStackTrace();
}
}
//Function First Method
public static Drawable loadImageFromURL(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent(); // While Debugging After this line Error occurs
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
Log.i("Image", "No Image");
return null;
}
}
//Second Method
public static Bitmap getBitmapFromURL(String src){
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream(); // While Debugging After this line Error occurs
Bitmap MyBitmap = BitmapFactory.decodeStream(input);
return MyBitmap;
} catch (Exception e) {
e.printStackTrace();
Log.i("Image", "No Image");
return null;
}
}
//Async Task even tried this
private class ImageDownload extends AsyncTask<String, String, Drawable>{
@Override
protected Drawable doInBackground(String... str) {
Drawable image = loadImageFromURL(str[0]);
return image;
}
}
I used the above 3 methods for 1st two methods i get the below error and no image view and async task shows no error and no image
My error
01-09 18:08:15.294: I/Image(1221): Image Null
01-09 18:08:15.315: W/System.err(1221): android.os.NetworkOnMainThreadException
01-09 18:08:15.315: W/System.err(1221):at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-09 18:08:15.315: W/System.err(1221): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-09 18:08:15.315: W/System.err(1221): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-09 18:08:15.324: W/System.err(1221): at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-09 18:08:15.324: W/System.err(1221): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-09 18:08:15.324: W/System.err(1221): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-09 18:08:15.324: W/System.err(1221): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
01-09 18:08:15.334: W/System.err(1221): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-09 18:08:15.334: W/System.err(1221): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-09 18:08:15.345: W/System.err(1221): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-09 18:08:15.345: W/System.err(1221): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-09 18:08:15.345: W/System.err(1221): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-09 18:08:15.354: W/System.err(1221): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-09 18:08:15.364: W/System.err(1221): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-09 18:08:15.364: W/System.err(1221): at in.datumdata.weather.MainActivity.getBitmapFromURL(MainActivity.java:109)
01-09 18:08:15.364: W/System.err(1221): at in.datumdata.weather.MainActivity$JSONParse.onPostExecute(MainActivity.java:187)
01-09 18:08:15.374: W/System.err(1221): at in.datumdata.weather.MainActivity$JSONParse.onPostExecute(MainActivity.java:1)
01-09 18:08:15.374: W/System.err(1221): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-09 18:08:15.374: W/System.err(1221): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-09 18:08:15.384: W/System.err(1221): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-09 18:08:15.384: W/System.err(1221): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 18:08:15.384: W/System.err(1221): at android.os.Looper.loop(Looper.java:137)
01-09 18:08:15.397: W/System.err(1221): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 18:08:15.404: W/System.err(1221): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:08:15.404: W/System.err(1221): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:08:15.414: W/System.err(1221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 18:08:15.414: W/System.err(1221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 18:08:15.414: W/System.err(1221): at dalvik.system.NativeStart.main(Native Method)
I want 3 images to be retrived from the json file Help me to solve this issue Thanks in advance
AsyncTask - To execute a long running task and prevent the ui from blocking by this long running process.
doInBackground - Method to do the background work which cannot make any changes in the UI
OnPostExecute -Method to make any changes in the UI
Network OnMainThread Exception:
You are getting the image in onPostExecute which should come in doInBackground
and from doInBackground pass the image to onPostExecute or save the downloaded bitmap in a static bitmap variable which can be used in onPostExecute and use that bitmap.