Java Android凌空向服务器发布json数据

Java Android凌空向服务器发布json数据,java,android,json,webserver,android-volley,Java,Android,Json,Webserver,Android Volley,我是Java新手。我想将post json数据发送到Web服务器。 我的截击帖子如下 public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){ RequestQueue requstQueue = Volley.newRequestQueue(mContext); JsonObjectRequest jsonobj = new JsonObjectRequest(

我是Java新手。我想将post json数据发送到Web服务器。 我的截击帖子如下

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);

}
我想将JSON数据发布到我的PostData方法中。 如何将此json数据发布到我的服务器?

public void postData(字符串url、最终JSONObject数据、最终截击回调mResultCallback){
    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);

    }
RequestQueue requstQueue=Volley.newRequestQueue(mContext); JsonObjectRequest jsonobj=新的JsonObjectRequest(Request.Method.POST,url,null, 新的Response.Listener(){ @凌驾 公共void onResponse(JSONObject响应){ if(mResultCallback!=null){ mResultCallback.notifySuccess(响应); } } }, 新的Response.ErrorListener(){ @凌驾 公共无效onErrorResponse(截击错误){ if(mResultCallback!=null){ mResultCallback.notifyError(错误); } } } ){ @凌驾 受保护的映射getParams(){ Map params=新的HashMap(); 如果(数据!=null){ 迭代器keysItr=data.keys(); while(keysItr.hasNext()){ String key=keysItr.next(); 对象值=data.get(键); if(JSONArray的值实例){ 值=toList((JSONArray)值); } else if(JSONObject的值实例){ 值=toMap((JSONObject)值); } 参数put(键、值); } } 返回参数; } }; 添加(jsonobj);
这是我的工作代码

希望这对你有用

快乐编码

公共void postData(字符串url、JSONObject数据、最终截击回调mResultCallback){
public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){
RequestQueue requestQueue = Volley.newRequestQueue(mContext);

JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,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);
                }
            }
        }
);
requstQueue.add(jsonobj);

}
RequestQueue RequestQueue=Volley.newRequestQueue(mContext); JsonObjectRequest jsonobj=新的JsonObjectRequest(Request.Method.POST、url、数据、, 新的Response.Listener(){ @凌驾 公共void onResponse(JSONObject响应){ if(mResultCallback!=null){ mResultCallback.notifySuccess(响应); } } }, 新的Response.ErrorListener(){ @凌驾 公共无效onErrorResponse(截击错误){ if(mResultCallback!=null){ mResultCallback.notifyError(错误); } } } ); 添加(jsonobj); }
新建JSONObject请求(Request.Method.POST,url,null,
第三个参数,如果用于JSONObject参数。用JSONDATA替换null这不能解决要求(在JSON OBJ中传递null)我的Android研究没有解析数据。key()、toList((JSONArray)值)和toMap((JSONObject)值);方法
    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
public void postData(String url,final 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);
                    }
                }
            }
    ){
       @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                if(data != null){
                    Iterator<String> keysItr = data.keys();
                    while(keysItr.hasNext()) {
                            String key = keysItr.next();
                            Object value = data.get(key);

                            if(value instanceof JSONArray) {
                                value = toList((JSONArray) value);
                            }

                            else if(value instanceof JSONObject) {
                                value = toMap((JSONObject) value);
                            }
                            params.put(key, value);
                        }
                } 
                return params;
            }
    };
    requstQueue.add(jsonobj);
public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){
RequestQueue requestQueue = Volley.newRequestQueue(mContext);

JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,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);
                }
            }
        }
);
requstQueue.add(jsonobj);

}