• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
浅时光
我喜欢,驾驭着代码在风驰电掣中创造完美!我喜欢,操纵着代码在随必所欲中体验生活!我喜欢,书写着代码在时代浪潮中完成经典!每一段新的代码在我手中诞生对我来说就象观看刹那花开的感动!
博客园    首页    新随笔    联系   管理    订阅  订阅

Android 多线程通信 访问网络

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";
    }
}

 

posted @ 2015-09-17 00:03  浅时光  阅读(306)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3