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

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下载

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值