博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Volley用法
阅读量:5109 次
发布时间:2019-06-13

本文共 6729 字,大约阅读时间需要 22 分钟。

private void doClockInOut(JSONObject params) {        Response.Listener
okListener = new Response.Listener
() { @Override public void onResponse(JSONObject response) { try { Log.e(service, response.getString("Status")); if ("1".equals(response.getString("Status"))) { success = true; } else if ("-4".equals(response.getString("Status"))) { View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { PersistenceUtils.getSharedInstance().put("login", "0"); MainActivity.act.startActivity(new Intent(MainActivity.act, LoginActivity.class)); MainActivity.act.finish(); } }; MessageDialog.showMessageDialogTwoButton(MainActivity.act, response.getString("Message"), "Cancel", "Login", listener); } else { error = true; //MessageDialog.showMessageDialog(MainActivity.act, response.getString("Message")); } } catch (Exception e) { e.printStackTrace(); } } }; VolleyUtils.getSharedInstance().init(MainActivity.act); VolleyUtils.getSharedInstance().requestService(MainActivity.act.getString(R.string.ws_url) + service + ".json", params, okListener, errorListener); } private void doOther(Map
params) { Response.Listener
okListener = new Response.Listener
() { @Override public void onResponse(String response) { try { Log.e(service, response); if ("true".equals(response)) { success = true; } } catch (Exception e) { Log.e(service, e.toString()); } } }; VolleyUtils.getSharedInstance().init(MainActivity.act); VolleyUtils.getSharedInstance().requestService1(MainActivity.act.getString(R.string.ws_url) + service + ".json", params, okListener, errorListener); } Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (VolleyUtils.getSharedInstance().getMessage(error) != null) Log.e(service, VolleyUtils.getSharedInstance().getMessage(error)); } };

  

package com.baclock.utils;import android.app.Activity;import android.content.Context;import com.android.volley.AuthFailureError;import com.android.volley.DefaultRetryPolicy;import com.android.volley.NetworkError;import com.android.volley.NoConnectionError;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.ServerError;import com.android.volley.TimeoutError;import com.android.volley.VolleyError;import com.android.volley.toolbox.JsonObjectRequest;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.baclock.R;import org.json.JSONArray;import org.json.JSONObject;import java.util.Map;/** * Created by Jack on 1/13/2016. */public class VolleyUtils {    private static VolleyUtils sharedInstance = null;    private RequestQueue requestQueue = null;    private String tag;    private int time_out_ms = 10000;    public static VolleyUtils getSharedInstance() {        if (sharedInstance == null) {            sharedInstance = new VolleyUtils();        }        return sharedInstance;    }    public void init(Activity act) {        if (requestQueue == null) {            requestQueue = Volley.newRequestQueue(act);            tag = act.getString(R.string.app_name);        }    }    public void requestService(String url, JSONObject param, Response.Listener
OkListener, Response.ErrorListener errorListener) { JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.POST, url, param, OkListener, errorListener); jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(time_out_ms, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); jsonObjRequest.setTag(tag); requestQueue.add(jsonObjRequest); } public void JsonArrayRequestService(String url, JSONObject param, Response.Listener
OkListener, Response.ErrorListener errorListener) { JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, url, param, OkListener, errorListener); request.setRetryPolicy(new DefaultRetryPolicy(time_out_ms, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); request.setTag(tag); requestQueue.add(request); } public void requestService1(String url, final Map
param, Response.Listener
OkListener, Response.ErrorListener errorListener) { StringRequest stringRequest = new StringRequest(Request.Method.POST, url, OkListener, errorListener) { @Override protected Map
getParams() { return param; } }; stringRequest.setRetryPolicy(new DefaultRetryPolicy(time_out_ms, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); stringRequest.setTag(tag); requestQueue.add(stringRequest); } public String getMessage(VolleyError error) { if (error instanceof TimeoutError) { return "The request time out!"; } else if (isServerProblem(error)) { return "Server is error"; } else if (isNetworkProblem(error)) { return "Network is error, please check your internet!"; } else if (error.getMessage().startsWith("org.json.JSONException")) { return null; } else {// if (error != null && error.getMessage() != null)// return error.getMessage();// else// return "Carrot error"; return error.getMessage(); } } private boolean isNetworkProblem(Object error) { return (error instanceof NetworkError) || (error instanceof NoConnectionError); } private boolean isServerProblem(Object error) { return (error instanceof ServerError) || (error instanceof AuthFailureError); }}

  

转载于:https://www.cnblogs.com/fanhq/p/5488382.html

你可能感兴趣的文章
湖北省软件行业协会会员单位全名录(2014年的信息)
查看>>
PAT A+B for Polynomials[简单]
查看>>
0601 Spring2.0 发布会
查看>>
EFCore执行Sql语句的方法:FromSql与ExecuteSqlCommand
查看>>
C++语言-02-函数
查看>>
jQuery5~7章笔记 和 1~3章的复习笔记
查看>>
centos7安装并配置nginx+php
查看>>
Linux下搭建DNS服务器
查看>>
(转载)PHP array_slice() 函数
查看>>
ZOOKEEPER之WATCHER简介
查看>>
[CF535D]Tavas and Malekas 题解
查看>>
cookie和localStorage、sessionStorage的区别
查看>>
关于工作流引擎授权问题的需求变更
查看>>
函数的作用域与匿名函数
查看>>
控制台输出文字颜色的方法
查看>>
百度前端学习日记14——面向对象
查看>>
关于中断处理程序的运行问题
查看>>
C#中int32 的有效值范围
查看>>
js只能输入数字和小数点
查看>>
nyoj 20 吝啬的国度
查看>>