大家好,我正在尝试从我的 MSQL 数据库中检索产品信息。价格和标题确实可以得到但不是图像。我不断收到 NetworkOnMainThread 错误。我知道这是因为代码在 runOnUiThread 中,因此是主线程。但是,一旦我删除 runOnUIThread 并且只有一个新的可运行代码,里面的代码无法执行,我就尝试了所有可能的解决方案,请帮忙?任何解决方案都将不胜感激。
// 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+"\n");
}
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();