Hi everyone am trying to retrieve product information from my MSQL database. The price and title does work to get however not the image. I keep getting the NetworkOnMainThread error. I know this is because the code is in runOnUiThread thus the main thread. But I tried all possible solutions once I remove runOnUIThread and only have a new runnable the code inside doesn't execute please help? any solution is grateful.
// TODO Auto-generated method stub
Tread loadingThread = new Thread(){
String result = "";
@Override
public void run() {
// TODO Auto-generated method stub
try{
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpentity=response.getEntity();
InputStream inputStream = httpentity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"),8);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine())!=null){
stringBuilder.append(line+"
");
}
inputStream.close();
result=stringBuilder.toString();
JSONArray Array = new JSONArray(result);
JSONObject jsonObject=null;
jsonObject = Array.getJSONObject(0);
String productTitle = jsonObject.getString("title");
String productPrice = jsonObject.getString("price");
final String productImage = jsonObject.getString("image_url");
productTextViewPrice.setText(productPrice);
productTextViewTitle.setText(productTitle);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
try {
InputStream is = (InputStream) new URL(productImage).getContent();
Log.i("log_URL","URL is " + productImage);
Drawable proImage = Drawable.createFromStream(is, "src name");
productImageFull.setImageDrawable(proImage);
} catch (Exception e) {
Log.i("log_Result","error getting image " + e.toString());
}
}
});
} catch (Exception e){
}
super.run();
}
};
loadingThread.start();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…