Http请求之android-async-http 异步框架请求
2015-08-07 16:17
639 查看
Android-async-http框架十分好用,而且他的优点在于能很好管理项目开发中的接口。
先下载这个框架Github地址:
https://github.com/loopj/android-async-http
或是直接到我上传的这个地址下面下载这个jar包
http://download.csdn.net/detail/u011539882/8973599
准备好了以后将android-async-http 引入到工程中,这里就不再阐述如何引入
利用这个开框架,有两部,第一步创建一个URL类或是叫做接口管理类,用于存放所有请求的地址,第二部在你的activity里面或其他地方引用这个管理类来实现网络请求。
这次我们来做一个搜索”琴吹柚”关键字的请求并显示页面文字
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.netrequestdemo.MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/main_tv" android:layout_width="match_parent" android:layout_height="5000dp" /> </LinearLayout> </ScrollView> </RelativeLayout>
Url管理类:
package com.example.netrequestdemo; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.JsonHttpResponseHandler; import com.loopj.android.http.RequestParams; /** * Url管理类这里只写了一个,实际开发中有很多请求 * */ public class URLManage { // 以百度搜索为例 //https://www.baidu.com/s?wd= // https://www.baidu.com/s?wd=琴吹柚 public final static String HOST = "https://www.baidu.com/"; private static AsyncHttpClient client = new AsyncHttpClient(); // 实例化对象 static { client.setTimeout(11000); // 设置链接超时,如果不设置,默认为10s } public static void showInfos(String string, JsonHttpResponseHandler res) { String urlString = HOST + "s"; RequestParams params = new RequestParams(); // 绑定参数 params.put("wd", string);// 前面的key就是连接中所对应的字段 get(urlString, params, res); } /** * 拼接地址并请求 * * @param urlString * @param params * @param res */ private static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) { System.out.println((urlString + "?" + params.toString()));//可以看下请求的地址 client.get(urlString, params, res); } }
MainActivity
package com.example.netrequestdemo; import org.apache.http.Header; import org.json.JSONObject; import com.loopj.android.http.JsonHttpResponseHandler; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView main_tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); main_tv = (TextView) findViewById(R.id.main_tv); //网络请求 new JsonHttpResponseHandler并重写里面的两个方法 URLManage.showInfos("琴吹柚", new JsonHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, String responseBody) { // TODO Auto-generated method stub super.onSuccess(statusCode, headers, responseBody); // System.out.println(responseBody.toString());//请求的网页内容文字形式 main_tv.setText(responseBody.toString());//设置内容 } @Override public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject errorResponse) { super.onFailure(statusCode, headers, e, errorResponse); Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show(); } }); } }
运行效果:
Demo下载
相关文章推荐
- HTTP Header 详解
- HttpContext.Current.User.Identity.Name获得不了登录名
- hdu 4046 2011北京赛区网络赛G 线段树 ***
- TCP连接分组交换状态图显示
- 基于java调用https接口
- 设置HttpSendRequest阻塞时间
- ios网络请求 get——post 区别
- HTTP跨域调用-传入URL就直接返回回来数据
- HttpClient异步调用WEB服务
- C++ http socket 文件上传和下载 FILE写文件失败
- TCP状态转换图详解
- TCP学习(1)--报文段首部格式
- http和socket之长连接和短连接区别
- python网络爬虫抓取ajax动态网页数据:以抓取KFC门店地址为例
- 协议森林08 不放弃 (TCP协议与流通信)
- http协议理解
- Chocolate 网络流解法
- 使用apache搭建http文件服务器
- 【VMCloud云平台】SCVMM进阶篇(一)网络虚拟化(1)
- Android 网络通信框架Volley简介(Google IO 2013)