Http请求之android-async-http 异步框架请求

来源:互联网 发布:java常用设计模式详解 编辑:程序博客网 时间:2024/06/05 17:28

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;@Overrideprotected 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(){@Overridepublic void onSuccess(int statusCode, Header[] headers, String responseBody) {// TODO Auto-generated method stubsuper.onSuccess(statusCode, headers, responseBody);//System.out.println(responseBody.toString());//请求的网页内容文字形式main_tv.setText(responseBody.toString());//设置内容}@Overridepublic 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下载

1 0