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

Android异步访问网络框架android-async-http使用

2014-07-21 15:10 519 查看
官网:点击下载

android-async-http开源框架可以是我们轻松的获取网络数据或者向服务器发送数据,使用起来也很简单,下面做简单介绍,具体详细使用看官网:

1.新建项目,去官网下载zip包,解压,打开releases文件,把里面最新的jar包,考入项目工程libs目录下,引入包。

2.通过1,就可以使用了,很简单,下面是自己写的demo,用它提供的各种不同方法完成从服务器获取一个json数据:

package com.http;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class HttpUtil {
private static AsyncHttpClient client =new AsyncHttpClient(); //实例话对象
static
{
client.setTimeout(11000); //设置链接超时,如果不设置,默认为10s
}
public static void get(String urlString,AsyncHttpResponseHandler res) //用一个完整url获取一个string对象
{
client.get(urlString, res);
}
public static void get(String urlString,RequestParams params,AsyncHttpResponseHandler res) //url里面带参数
{
client.get(urlString, params,res);
}
public static void get(String urlString,JsonHttpResponseHandler res) //不带参数,获取json对象或者数组
{
client.get(urlString, res);
}
public static void get(String urlString,RequestParams params,JsonHttpResponseHandler res) //带参数,获取json对象或者数组
{
client.get(urlString, params,res);
}
public static void get(String uString, BinaryHttpResponseHandler bHandler) //下载数据使用,会返回byte数据
{
client.get(uString, bHandler);
}
public static AsyncHttpClient getClient()
{
return client;
}
}这个类主要列出了我们常用的get方法,在要使用的地方,调用该类就行了。

具体使用的类:<ScrollView
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:orientation="vertical"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:textColor="#FF0000"
android:textSize="16sp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取数据完成后会显示在这里" />
<Button
android:layout_marginTop="50dp"
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一种"
android:onClick="method1"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2种"
android:onClick="method2"
/>
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3种"
android:onClick="method3"
/>
<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4种"
android:onClick="method4"
/>
<Button
android:id="@+id/bt5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5种"
android:onClick="method5"
/>
<TextView
android:textColor="#FF0000"
android:textSize="16sp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>记得加入网文网络的权限和向sdcard的访问权限
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息