5

【求助】关于android handle的问题毕设急求帮助。

开启了一个子线程,其中有两个方法分别负责从服务器下载图片和文字。下载成功后,两个方法中都分别创建了message来将数据传到主线程中的handlemessage()方法处理,第一个下载图片的方法没问题第二个下载文字后调用sendmessage时程序异常退出。求解? 代码如下:

public class Query extends Activity {
String image_path = "http://img.ivsky.com/img/bizhi/pic/201504/27/benpaoba_xiongdi_dierji-001.jpg";
Bitmap b;
ImageView show;
ProgressDialog dialog;
private final int IS_FINISH = 3;
private final int IS_OK = 6;
private static final String TAG = "Query";
Handler handler = new Handler() {
public void handleMessage(Message msg) {
byte[] data = (byte[]) msg.obj;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
show.setImageBitmap(bitmap);
if(msg.what == IS_FINISH) {
dialog.dismiss();
}
if(msg.what == IS_OK) {
String s = (String) msg.obj;
}

}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
Button download = (Button) this.findViewById(R.id.download);
show = (ImageView) this.findViewById(R.id.imageView_download);
download.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
new Thread(new MyThread()).start();

}

});

}

public class MyThread implements Runnable {
    public void run() {
        downloadPicture();
        showTest();
    }
}
public void downloadPicture() {
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(image_path);
    HttpResponse httpResponse = null;
    try {
        httpResponse = client.execute(httpGet);
        if(httpResponse.getStatusLine().getStatusCode() == 200) {
            byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
            Message message = Message.obtain();
            message.obj = data;
            message.what = IS_FINISH;
            handler.sendMessage(message);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void showTest() {
    String url_path = "http://192.168.1.94:8080/TestSysClient/servlet/AndroidServlet";
    String jsonString = HttpTools.getJsonContent(url_path);
    List<Device> list = JsonTools.getDeviceList("devices", jsonString);
    System.out.println(jsonString);
    Log.i(TAG, list.toString());
    Message ms = Message.obtain();
    Bundle data = new Bundle();
    data.putString("message", jsonString);
    ms.setData(data);
    ms.what = IS_OK;
    ms.obj = "haha";
    handler.sendMessage(ms);       //加上此处后程序出现问题。
}


如何解决问题呢?
查看全部
zwmmenghuan123
zwmmenghuan123
2015/05/05 15:30
  • 线程
  • android
  • 下载
  • handle
  • message
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复