Android Volley将json数据发布到服务器 [英] Android Volley post json data to server

查看:103
本文介绍了Android Volley将json数据发布到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手.我想将json数据发送到Web服务器. 我的Volley帖子如下.

I am new in Java. I want to send post json data to webserver. My Volley post is given below.

public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){
    RequestQueue requstQueue = Volley.newRequestQueue(mContext);

    JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if(mResultCallback != null){
                        mResultCallback.notifySuccess(response);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(mResultCallback != null){
                        mResultCallback.notifyError(error);
                    }
                }
            }
    ){
      //here I want to post data to sever
    };
    requstQueue.add(jsonobj);

}

这是我的MainActivity代码

Here is my MainActivity code

JSONObject data = null;
            try {
                String datas = "{'email': email,'password': password}";
                data = new JSONObject(datas);
            }catch (JSONException e){
                e.printStackTrace();
            }
String url = "http://example.com";

我想将JSON数据发布到我的PostData方法中. 如何将该json数据发布到服务器上?

I want to post JSON data to my PostData method. How can I post this json data to my server?

推荐答案

    public void postData(String url,Hashmap data,final VolleyCallback mResultCallback){
        RequestQueue requstQueue = Volley.newRequestQueue(mContext);

        JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(data),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if(mResultCallback != null){
                            mResultCallback.notifySuccess(response);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if(mResultCallback != null){
                            mResultCallback.notifyError(error);
                        }
                    }
                }
        ){
          //here I want to post data to sever
        };
        requstQueue.add(jsonobj);

    }

现在,从您的mainActiviy类中

Now, from your mainActiviy class

    Hashmap data = new HashMap();
    data.put("email","email");
    data.put("password","password");      
    String url = "http://example.com";

//now you can just call the method, I have rectified your string to hashmap,
postData(url,data,new mResultCallb.....              //rest of your code

这篇关于Android Volley将json数据发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆