Android 多线程通信 访问网络

时间:2023-01-23 21:32:50
package org.rongguang.testthread;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import java.io.IOException; public class MainActivity extends Activity { private static final String TAG = "MainThread";
private Handler mMainHandler, mChildHandler;
private EditText goods;
private TextView info;
private Button msgBtn; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); info = (TextView) findViewById(R.id.info);
msgBtn = (Button) findViewById(R.id.msgBtn);
goods= (EditText) findViewById(R.id.goods); mMainHandler = new Handler() { @Override
public void handleMessage(Message msg) {
Log.i(TAG, "Got an incoming message from the child thread - "
+ (String) msg.obj);
// 接收子线程的消息
info.setText((String) msg.obj);
} }; new ChildThread().start(); msgBtn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { if (mChildHandler != null) { //发送消息给子线程
Message childMsg = mChildHandler.obtainMessage();
childMsg.obj = goods.getText().toString().trim();
mChildHandler.sendMessage(childMsg); Log.i(TAG, "Send a message to the child thread - " + (String)childMsg.obj); }
}
}); } public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Stop looping the child thread's message queue"); mChildHandler.getLooper().quit();
} class ChildThread extends Thread { private static final String CHILD_TAG = "ChildThread"; public void run() {
this.setName("ChildThread"); //初始化消息循环队列,需要在Handler创建之前
Looper.prepare(); mChildHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(CHILD_TAG, "Got an incoming message from the main thread - " + (String) msg.obj); // try {
//
// //在子线程中可以做一些耗时的工作
// sleep(100); Message toMain = mMainHandler.obtainMessage();
// toMain.obj = "This is " + this.getLooper().getThread().getName() +
// ". Did you send me \"" + (String)msg.obj + "\"?";
if(msg.obj!=null) {
toMain.obj = getNet((String)msg.obj);
mMainHandler.sendMessage(toMain);
}
Log.i(CHILD_TAG, "Send a message to the main thread - " + (String) toMain.obj); // } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} }; Log.i(CHILD_TAG, "Child handler is bound to - " + mChildHandler.getLooper().getThread().getName()); //启动子线程消息循环队列
Looper.loop();
}
}
public String getNet(String msg){
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet("http://fanyi.youdao.com/openapi.do?keyfrom=rongguangfdasds&key=498519883&type=data&doctype=json&version=1.1&q="+msg);
try {
HttpResponse httpResponse= httpClient.execute(httpGet);
String result= EntityUtils.toString(httpResponse.getEntity());
Log.i("Res===",result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return "0";
}
}