XUtils之HttpUtils
来源:互联网 发布:windows api编程 编辑:程序博客网 时间:2024/06/05 02:31
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(); } }); }}
0 0
- xUtils系列之HttpUtils
- Xutils之HttpUtils使用
- XUtils之HttpUtils
- xUtils之HttpUtils的用法
- XUtils之BitmapUtils和HttpUtils
- XUtils源码分析之HttpUtils(一)
- xUtils框架介绍之HttpUtils的使用
- android高级框架xUtils之HttpUtils
- xUtils框架之HttpUtils模块分析
- Android学习(58) -- xUtils之HttpUtils
- android-----XUtils框架之HttpUtils源码分析
- Android Xutils框架使用之HttpUtils
- Android学习之xUtils --- HttpUtils模块
- 快速开发之xUtils(四)HttpUtils详细介绍
- Android:开源框架xutils介绍之 HttpUtils
- Android高级之xUtils框架(二):HttpUtils
- 简单使用xUtils-HttpUtils
- xutils的 httpUtils讲解
- HDU_2717_Catch That Cow
- 找出下一个比它大而且值为1的位元数与之相同的数字
- 文章标题
- unix线程同步
- 使用C#创建快捷方式
- XUtils之HttpUtils
- Java基础1
- Fleury算法求欧拉路径
- Android 自定义控件之圆点指示器 View (IndicateDotView)
- 关于int *a; int &a; int & *a; int * &a
- python 下载csv文件保存到 redis
- 下载和安装R、RStudio !~~~
- unix进程控制
- JavaSE 之 ----- Java线程池的解析