您的位置:首页 > 理论基础 > 计算机网络

网络通讯http

2015-09-24 00:00 561 查看
摘要: 网络通信 httpget 简易的HTTP异步框架

1.httpget获取数据

1.1利用AsyncTask读取数据

1.2创建URL

1.3创建URLConnection接受返回值

1.4创建InputStream返回值

1.5建立字符集 从字节到字符的转换 读取数据

1.6关闭

public void onClick(View v) {
// 网络耗时 采用异步任务进行获取
new AsyncTask<String, Void, Void>() { // 利用AsyncTask读取数据
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL(params[0]);
URLConnection connection = url
.openConnection(); // 创建URLConnection接受返回值
InputStream is = connection
.getInputStream();// 创建InputStream返回值
InputStreamReader isr = new InputStreamReader(
is, "UTF_8"); // 建立字符集 从字节到字符的转换

BufferedReader br = new BufferedReader(isr); // 读取一行字符串
String line;
&nbs
3ff0
p;                         while ((line = br.readLine()) != null) {
System.out.println(line); //打印在logCat zhong

}
br.close();
isr.close();
is.close();

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}.execute(geturl);//geturl为从中获取地址的网址


2.httpPost获取数据

2.1利用AsyncTask读取数据

2.2声明URL

2.3创建URLConnection 接受返回值

2.4确认权限。获取输出流

2.5写入数据 将数据传到数据库

2.6建立InputStream,建立字符集从字节到字符的转换

2.7关闭

public void onClick(View v) {
// 网络耗时 采用异步任务进行获取
new AsyncTask<String, Void, Void>() { // 利用AsyncTask读取数据
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection(); // 创建URLConnection接受返回值
connection.setDoOutput(true); //输出
connection.setDoInput(true);//输入
connection.setRequestMethod("POST");
OutputStreamWriter osw=new OutputStreamWriter(connection.getOutputStream(),"UTF-8");//获取输出流
BufferedWriter bw = new BufferedWriter(osw);
bw.write("keyfrom=yuyuyu&key=719067052&type=data&doctype=xml&version=1.1&q=这里是有道翻译API");
bw.flush();//将数据传入到数据库
InputStream is = connection
.getInputStream();// 创建InputStream返回值
InputStreamReader isr = new InputStreamReader(
is, "UTF_8"); // 建立字符集 从字节到字符的转换

BufferedReader br = new BufferedReader(isr); // 读取一行字符串
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);

}
br.close();
isr.close();
is.close();

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}.execute("http://fanyi.youdao.com/openapi.do");

}


3.简易的HTTP异步框架

3.1.开启子线程,执行一个http请求,在后台执行,在子线程中执行

3.2. 子线程执行完毕,通知UI 界面更新

public void click(View view) {
// 1.开启子线程,执行一个http请求,在后台执行,在子线程中执行
// 2. 子线程执行完毕,通知UI 界面更新
AsyncHttpClient client = new AsyncHttpClient();//开启一个子线程,并且把子线程的内容穿回到这里
String path = "http://210.29.185.218:8080/web/LoginServlet?username="
+ URLEncoder.encode("zhangsan") + "&password="
+ URLEncoder.encode("123");
client.get(path, new MyHander() { //根据不同的方法来确定是否成功,失败
public void doSuccess(String content) {
Toast.makeText(MainActivity.this, "成功", 0).show();
}
public void doFailure(String comtent) {
Toast.makeText(MainActivity.this, "失败", 0).show();
}
}
);
}


public class AsyncHttpClient {

public void get(final String path, final MyHander myHandler) {
new Thread(){
public void run(){
//创建一个HTTP请求,并且执行
HttpClient client  = new DefaultHttpClient();
HttpGet httpget = new HttpGet(path);
try {
HttpResponse response =client.execute(httpget);
InputStream is = response.getEntity().getContent();
String result = StreamTools.readInputStream(is);
//执行成功
Message msg = new Message();
msg.what =1;
msg.obj=result;
myHandler.sendMessage(msg);

} catch (IOException e) {
e.printStackTrace();
Message msg = new Message();
msg.what =2;
msg.obj="请求失败";
myHandler.sendMessage(msg);
}
}
}.start();
}


创建子线程,

Hander类

public class MyHander extends Handler {

public void doSuccess(String content) {

}

public void doFailure(String comtent) {

}
@Override
public void handleMessage(Message msg) {
String content = (String) msg.obj;
switch (msg.what) {
case 1:
doSuccess(content);
break;
case 2:
doFailure(content);
break;
}
}
}


public class StreamTools {

public static String readInputStream(InputStream is) {
// TODO Auto-generated method stub
return
3ff0
null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息