volley实现传入string 返回JsonObject

Posted 石墨方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了volley实现传入string 返回JsonObject相关的知识,希望对你有一定的参考价值。

转自 https://blog.csdn.net/lining1041204250/article/details/73322108

在 主界面 

RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);
String url = "http://localhost/a/login";
HashMap<String, String> hashMap = new HashMap<String,String>();
        hashMap.put("name", "123456");
        hashMap.put("password", "123456");

        JSONObject jsonObject = new JSONObject(hashMap);

        CustomRequest  mRequest = new CustomRequest (Request.Method.POST,url,hashMap,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            String rs = response.getString("result");
                            Log.i("返回数据:",rs.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }) {
        };
        mQueue.add(mRequest);

 

重写JSONObjectRequest

 

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public CustomRequest(int method, String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

 

就搞定啦

以上是关于volley实现传入string 返回JsonObject的主要内容,如果未能解决你的问题,请参考以下文章

Androiid_Volley+Image-Loader+RecyclerView实现网络下载图片瀑布流

android volley stringrequest post中的getparams怎么把json数据提交上去

利用Volley框架实现简单的Android与servlet信息交互

(Volley) Post 方法后如何处理返回的 JsonObject

Android_Volley+Image-Loader+RecyclerView实现网络下载图片瀑布流

Android_Volley+Image-Loader+RecyclerView实现网络下载图片瀑布流