异步http框架(get)(无服务器)
来源:互联网 发布:sql 字符串聚合函数 编辑:程序博客网 时间:2024/05/16 02:37
<uses-permission android:name="android.permission.INTERNET"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="发送http请求" />
StreamTools
package org.gentry.asynchttp.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTools {/** * 把输入流的内容转化成字符串 * * @param is * @return */public static String readInputStream(InputStream is) {try {ByteArrayOutputStream baos = new ByteArrayOutputStream();int len = 0;byte[] buffer = new byte[1024];while ((len = is.read(buffer)) != -1) {baos.write(buffer, 0, len);}is.close();byte[] result = baos.toByteArray();// 试着解析result里面的字符串String temp = new String(result);// 处理字符乱码问题if (temp.contains("UTF-8")) {return temp;} else if (temp.contains("gb2312")) {return new String(result, "gb2312");}return temp;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return "获取失败";}}}MyHandler
package org.gentry.asynchttp;import android.os.Handler;import android.os.Message;public class MyHandler extends Handler {public void onFailure(String content) {}public void onSuccess(String content) {}@Overridepublic void handleMessage(Message msg) {String content = (String) msg.obj;switch (msg.what) {case 1:onSuccess(content);break;case 2:onFailure(content);break;}}}AsyncHttpClient
package org.gentry.asynchttp;import java.io.InputStream;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.gentry.asynchttp.utils.StreamTools;import android.os.Message;public class AsyncHttpClient {public void get(final String path, final MyHandler myHandler) {new Thread() {public void run() {HttpClient client = new DefaultHttpClient();HttpGet httpGet = new HttpGet(path);try {HttpResponse response = client.execute(httpGet);InputStream is = response.getEntity().getContent();String content = StreamTools.readInputStream(is);// 执行成功Message msg = new Message();msg.what = 1;msg.obj = content;myHandler.sendMessage(msg);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();// 执行失败Message msg = new Message();msg.what = 2;msg.obj = "请求失败";myHandler.sendMessage(msg);}};}.start();}}MainActivity
package org.gentry.asynchttp;import java.net.URLEncoder;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void click(View view) {// 1. 开启子线程,执行一个http请求,在后台执行,在子线程执行// 2. 子线程执行完毕后,通知UI界面更新AsyncHttpClient client = new AsyncHttpClient();String path = "http://192.168.1.100:8080/web/LoginServlet?username="+ URLEncoder.encode("zhangsan") + "&password="+ URLEncoder.encode("123");client.get(path, new MyHandler() {@Overridepublic void onFailure(String content) {Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();super.onFailure(content);}@Overridepublic void onSuccess(String content) {Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();super.onSuccess(content);}});}}
0 0
- 异步http框架(get)(无服务器)
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- 采用get方式提交数据到服务器(无服务器)
- iOS 网络编程(HTTP 同步GET请求,同步POST请求,异步GET请求,异步POST请求)
- iOS 网络编程(HTTP 同步GET请求,同步POST请求,异步GET请求,异步POST请求)
- 采用httpclient提交数据到服务器(get/post)(无服务器) → 采用get方式提交数据到服务器
- Http与服务器的交互(主要post和get)
- Get访问HTTP服务器
- NSURLConnection--异步下载HTTP Get
- Androidx学习笔记(40)--- 使用异步HttpClient(android-async-http-master)做get提交
- nodejs 5 nodejs的web应用-http服务器(http, 路由,get,post,https)
- (无)状态服务器
- HTTP条件Get(Conditional Get)
- HTTP条件Get(Conditional Get)
- 采用post的方式提交数据(无服务器) →采用get方式提交数据到服务器
- iOS原生Http请求,get post 异步
- 总结http服务器框架
- 可扩展多线程异步Socket服务器框架EMTASS 2.0(来源:http://blog.csdn.net/hulihui)
- Intent及其七大属性及intent-filter设置
- 硬盘分区表知识——详解硬盘MBR
- hdu 4911 Inversion(归并)
- POJ 2240 Arbitrage Floyd || Bellman
- 047:QUESTION 228
- 异步http框架(get)(无服务器)
- vim/vi
- JVM Server与Client运行模式
- 用iftop监控网络流量
- Android实现换肤功能(一)
- android一个项目应用另外一个项目和jar包引用
- NSFileHandle 类 不能创建文件
- 安装MongoDB
- [leetcode 21] Remove Nth Node From End of List