Somehow I don’t seem to get it. 8-}
I have an activity in which I request JSON data from a database, an the return result includes an image URL. My app works fine on Android v2.x, but as I had to find out by accident, there is weird behavior – including the images not loaded – on Android 4 (that’s not what I’d call compatibility?!) Now I have to fix that, hoping the fix will not mess up functionality on Android 2.x systems again!
Basically, the activity works as described in all the numerous examples on the web (I removed most error checking etc. to trim the code down to the essential parts):
new GetProductDetails().execute();
(..)
class GetProductDetails extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
(..)
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
getString(R.string.urlProductDetails), "GET",
params);
try {
JSONArray prodObj = json.getJSONArray(Consts.PRODS);
pinfo = prodObj.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Storing each json item in variable
try {
String s = "";
txtvname = (TextView) findViewById(R.id.txtpname);
txtshortinfo = (TextView) findViewById(R.id.txtshortinfo);
(… populating activity window with json data …)
}
});
// update image:
String imgURL;
try {
imgURL = pinfo.getString("pic_url");
if (imgURL.length() > 1) {
imgURL = getString(R.string.urlPicsFull) + imgURL;
ImageView imgProd = (ImageView) findViewById(R.id.imgProduct);
Drawable drawable = LoadImageFromWebOperations(imgURL);
imgProd.setImageDrawable(drawable);
}
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
}
private Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Could not load " + url + ", Exc=" + e);
return null;
}
}
Now, there is an android.os.NetworkOnMainThreadException when the image is loaded.
I suppose your targetSDKVersion is too high. Here is some documentation on what compatibility behaviour is disabled on which targetSDKVersion. NetworkOnMainThreadException is actually missing from that list. It is documented here as follows: “This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged.”
So if you lower your targetSDKVersion to a value below Honeycomb you can do networking on the main thread. Setting targetSDKVersion to a certain value is like saying: “I tested my application on that level.”