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";
}
}
Android 多线程通信 访问网络的更多相关文章
- android post 方式 访问网络 实例
android post 方式 访问网络 实例 因为Android4.0之后对使用网络有特殊要求,已经无法再在主线程中访问网络了,必须使用多线程访问的模式 该实例需要在android配置文件中添加 网 ...
- Android多线程通信机制
掌握Android的多线程通信机制,我们首先应该掌握Android中进程与线程是什么. 1. 进程 在Android中,一个应用程序就是一个独立的进程(应用运行在一个独立的环境中,可以避免其他应用程序 ...
- android 使用httpclient访问网络
在主活动类中,调用一个线程访问网络(android4.0以上耗时的操作不能放在主线程中): //声明两个Button对象,与一个TextView对象private TextView mTe ...
- Android多线程通信之Handler
主线程 public class MainActivity extends ActionBarActivity { private Handler handler; // private Thread ...
- Android 多线程注意事项
参考:http://blog.csdn.net/x86android/article/details/14161981 http://geeksun.iteye.com/blog/1447708 An ...
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- java多线程实现TCP网络Socket编程(C/S通信)
目录 开篇必知必会 一.多线程技术 二.实现多线程接收 1.单线程版本 2.多线程版本 三.多线程与进程的关系 四.客户端界面完整代码 五.多线程通信对比 最后 开篇必知必会 在前一篇<Java ...
- Android 使用 HTTP 协议访问网络
正在看<第一行代码>,记录一下使用 HTTP 协议访问网络的内容吧! 在Android发送Http请求有两种方式,HttpURLConnection和HttpClient. 1.使用Htt ...
- Android访问网络(可以正常使用)
以下是MainActiviy.java,有必要的注释,里面用到了handler,以及线程,workThread如何更新mainThread才能够更新的内容. package com.wyl.httpt ...
随机推荐
- Candies---hdu3159(spfa+差分约束)
题目链接:http://poj.org/problem?id=3159 题意:有n个小孩,m个关系格式是A B C 表示小孩 B 的糖果数最多比小孩A多C个,相当于B-A<=C; 有m个这样的关 ...
- http://blog.csdn.net/pi9nc/article/details/23169357
http://blog.csdn.net/pi9nc/article/details/23169357
- 搭建和使用Docker私有仓库
需要注意的是,从Docker Pool下载的镜像文件,与官方镜像文件是完全一致的. 安装Docker之后,可以是使用官方提供的registry镜像来搭建一套本地私有仓库环境: docker run ...
- 分享一下怎么开发一款图片视频类App,秒拍和prisma
第一步,分解短视频App的功能 我们在秒拍官网看到如此描述: [视频拍摄及导入]支持直接拍摄及导入手机本地的视频 [照片电影]照片专属特效,轻松创作照片电影 [MV特效]10余款全新MV特效,让普通视 ...
- Linux是怎么启动的
按下电源按钮的直到欢迎页出来之后,linux总共做的事可以分为五步来完成. 1. BIOS加电自检: 加电自检,检测硬件设备.然后按照cmos上面的顺序来搜索处在活动状态下的可以引导的设备.可以是光驱 ...
- Unity3D 插件大全
2D_Toolkit 2d动画开发插件包 FingerGestures 触摸插件 ORK_Okashi_RPG_Kit Unity3D角色扮演游戏开发工具包 uScript-Visual-Script ...
- iptables 基础知识
[root@tp ~]#iptables -L -n 查看防火墙规则 [root@tp ~]# iptables -D INPUT 1 根据命令iptables -L -n --line-numbe ...
- 成员变量的隐藏,方法的覆盖,super关键字
成员变量的隐藏:当父类和子类有相同的成员变量时,即定义了与父类相同的成员变量时,就会发生子类对父类变量的隐藏.对于子类的对象来说,父类中的同名成员变量被隐藏起来,子类就会优先使用自己的成员变量,父类成 ...
- [c++基本语法]——构造函数初始化列表
c++构造函数初始化成员变量列表: #pragma once class Node { public: int data; // 权值 Node *parent; // 父节点 Node *left; ...
- session 实现保存用户信息
index.jsp <body> <div style="margin: 0 auto; width: 500px; text-align: center;"&g ...