Android开发 使用Xutils3请求、解析数据

xiaoxiao2021-02-27  294

1、引进xutils3的包和Gosn包

2、建立MyApplication类:

public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); } }

并在配置文件将完成配置:

<application android:name="MyApplication"> 不要忘了这个哦: <uses-permission android:name="android.permission.INTERNET"/>

3、新建一个HttpUtils类,里面完成申请数据

public class HttpUtils { private static HttpUtils httpPost; public static HttpUtils getInstance() { if (httpPost == null) { httpPost = new HttpUtils(); } return httpPost; } public void postSend(final Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (!mobNetworkInfo.isConnected() && !wifiNetworkInfo.isConnected()) { Toast.makeText(context, "你已经进入没有网络的异次元世界", Toast.LENGTH_SHORT).show(); } else { x.http().post(onResult.onParams(), new Callback.CacheCallback<String>() { @Override public boolean onCache(String result) { return false; } @Override public void onSuccess(String result) { Log.e(TAG, "onSuccess: "+result); try { JSONObject object = new JSONObject(result); if (object.getString("status").equals("1")){ onResult.onUsage(result); }else { Toast.makeText(context, object.getString("info"), Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { onResult.onError(ex.toString()); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } } public void postSend() { x.http().post(onResult.onParams(), new Callback.CacheCallback<String>() { @Override public boolean onCache(String result) { return false; } @Override public void onSuccess(String result) { onResult.onUsage(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { System.out.println("请求出错..."); System.out.println(ex); onResult.onError(ex.toString()); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } public void getSend(final Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (!mobNetworkInfo.isConnected() && !wifiNetworkInfo.isConnected()) { Toast.makeText(context, "你已经进入没有网络的异次元世界", Toast.LENGTH_SHORT).show(); } else { x.http().get(onResult.onParams(), new Callback.CacheCallback<String>() { @Override public boolean onCache(String result) { return false; } @Override public void onSuccess(String result) { onResult.onUsage(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { System.out.println("请求出错..."); System.out.println(ex); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } } public interface OnResult { /** * @return 自定义的RequestParame请求参数 */ RequestParams onParams(); /** * 请求成功后的操作 * * @param result */ void onUsage(String result); /** * 请求出错后的操作 */ void onError(String error); } private OnResult onResult; public void setOnResult(OnResult onResult) { this.onResult = onResult; } }

4、请求数据

HttpUtils httpUtils1=new HttpUtils(); httpUtils1.setOnResult(new HttpUtils.OnResult() { @Override public RequestParams onParams() { RequestParams params=new RequestParams("请求数据的接口"); params.addParameter("字段一",值); params.addParameter("字段二",值); return params; } @Override public void onUsage(String result) { try { JSONObject object = new JSONObject(result); if (object.getInt("status") == 1) { Gson gson = new Gson(); DriverModel (对应的Bean文件,bean文件的建立就不细说了,你们可以使用Gosnformat插件快速建立Bean文件) DriverModel driverModel = new DriverModel(); driverModel = gson.fromJson(result, DriverModel.class); //假如是list for (int i = 0; i < driverModel.getDate().size(); i++) { if (driverModel.getDate().get(i).getStatus().equals("2")) { dateBeanList.add(driverModel.getDate().get(i)); } } } myAdapter = new OrderItemAdapter(getActivity(), dateBeanList); lvMyOrder.setAdapter(myAdapter); myAdapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(String error) { } }); httpUtils1.postSend(); }

5、显示数据:http://blog.csdn.net/qq_30299305/article/details/73864024

转载请注明原文地址: https://www.6miu.com/read-3127.html

最新回复(0)