XUtils之HttpUtils

本文介绍了一个基于HttpUtils库实现的简单网络数据请求示例。该示例展示了如何配置HttpUtils并使用GET方法从指定URL获取JSON数据,然后解析这些数据并将其展示在一个Android应用的ListView组件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HttpUtils请求网络数据

import java.util.List;
import com.example.week1_httputils.Car.Carmsg;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView lv;
    private String url = "http://172.17.29.120/localuser/lujunfang/hello/xiangmuyi_20161008_test1.json";
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
                String json = (String) msg.obj;
                Car car = new Gson().fromJson(json, Car.class);
                List<Carmsg> list = car.list;
                lv.setAdapter(new ArrayAdapter<Carmsg>(MainActivity.this,
                        android.R.layout.simple_list_item_1, list));
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        // 获得HttpUtils
        HttpUtils httpUtils = new HttpUtils();
        //设置响应结果的编码集格式
        httpUtils.configResponseTextCharset("utf-8");
        // 调用send方法 参数一:请求的方式;
        // 参数二:网址url;参数三:抽象类RequestCallBack<T>,泛型是需要的返回值类型(json--String,图片--Bitmap)
        // HttpUtils.send(HttpMethod method, String url, RequestCallBack<T> callBack)
        httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
      //如果一致不走onsuccess,那就在onfailure中找原因;还要看看error;-----------------------

            @Override//请求开始
        public void onStart() {
            super.onStart();
        }

            @Override//请求中
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
            }

            @Override
            // 请求失败---查询数据库
            public void onFailure(HttpException arg0, String arg1) {
                 System.out.println(arg0.toString()+"\n"+arg1);
            }

            @Override
            // 请求成功---获取数据
            public void onSuccess(ResponseInfo<String> arg0) {
                String json = arg0.result;// .result 的返回值类型与RequestCallBack<T>的泛型一致
                // --------------高大上----------------
                handler.obtainMessage(1, json).sendToTarget();
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值