android异步处理
来源:互联网 发布:ipad美女直播软件 编辑:程序博客网 时间:2024/05/16 01:52
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入电话号码"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mobienumber"
android:hint="电话号码"
/>
<Button
android:id="@+id/mobieBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="查询" />
</LinearLayout>
package com.csdn.hbsi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MobileselectActivity extends Activity {
private EditText etn;
private Button btn_sel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void click(View v){
etn = (EditText) findViewById(R.id.mobienumber);
btn_sel = (Button) findViewById(R.id.mobieBtn);
String url="http://www.096.me/api.php?phone="+etn.getText().toString().trim()+"&mode=txt";
System.out.println(url);
As ags=new As(getApplicationContext(),btn_sel);
ags.execute(url);
}
}
package com.csdn.hbsi;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpEntity;
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 android.content.Context;
import android.os.AsyncTask;
import android.widget.Button;
import android.widget.Toast;
public class As extends AsyncTask<String, Void,String>{
private Button btn_sel;
private Context context;
String getStr="";
public As(Context _context, Button btn){
this.btn_sel=btn;
this.context=_context;
}
@Override
protected String doInBackground(String... params){
System.out.println("执行中");
try {
Thread.sleep(3000);
HttpClient client= new DefaultHttpClient();
HttpGet get=new HttpGet(params[0]);
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity he =response.getEntity();
InputStream is=he.getContent();
getStr=new String(getByteData(is),"GBK");
System.out.println("==="+getStr);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return getStr;
}
@Override
protected void onPreExecute() {
btn_sel.setText("查询。。。");
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
btn_sel.setText("查询");
System.out.println("sadfasdfasdfasdfas ===="+result);
//显示一下
Toast.makeText(context, result, 1).show();
super.onPostExecute(result);
}
public static byte[] getByteData(InputStream is){
//声明一个ByteArrayOutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
//定义缓冲区
byte[] buffer = new byte[1024];
//读取is中的数据,写入到bos中
int len = -1;
while ((len = is.read(buffer)) != -1) {
//写入到bos中
bos.write(buffer, 0, len);
}
is.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
return bos.toByteArray();
}
}
- Android AsyncTask异步处理
- Android异步处理
- Android异步消息处理
- Android异步处理
- Android AsyncTask异步处理
- Android AsyncTask异步处理
- android异步处理
- Android实现异步处理
- Android AsyncTask异步处理
- android 异步处理
- Android异步消息处理
- Android AsyncTask异步处理
- Android异步处理
- Android AsyncTask异步处理
- Android异步处理
- android 异步消息处理
- Android异步消息处理
- Android AsyncTask异步处理
- 预编译(来自百度百科)
- Point and Pixel
- Hadoop 2.0 NameNode HA和Federation实践
- 同义词
- 字符串逆序
- android异步处理
- 在Ubuntu上安裝java7,在linux终端中用命令启动eclipse
- 程序猿的一些幽默
- 如何将所有的进程运行在一个指定的CPU上
- 【转】cygwin与MinGW区别
- bashrc 与 bashprofile文件区别
- dos下如何编译servlet
- poj 2781 The mysterious X network (BFS最短路)
- Qt Class Reference [QtCore module] Qt类参考手册-其他定义(类型常量) 翻译中