zwmmenghuan123
2015-05-05 15:30
采纳率: 33.3%
浏览 1.8k

【求助】关于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);       //加上此处后程序出现问题。
}


如何解决问题呢?
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • danielinbiti 2015-05-05 15:36
    已采纳
    ms.setData(data);
        ms.what = IS_OK;
        ms.obj = "haha";
        handler.sendMessage(ms);       //加上此处后程序出现问题。这里的obj是“haha"
    
    
    
     public void handleMessage(Message msg) {
    byte[] data = (byte[]) msg.obj;   //发送过来的"haha",转成byte数组,类型不对了。
    
    已采纳该答案
    打赏 评论
  • lrj1152665 2015-05-05 15:59

    楼上正确,不知道怎么出错了多看log的输出。

    打赏 评论
  • zwmmenghuan123 2015-05-06 01:14

    哦,我粗心了,谢谢两位。

    打赏 评论

相关推荐 更多相似问题