我想在volley调用中发送json对象作为参数

时间:2016-06-07 17:31:32

标签: android json android-studio android-volley

嗨我知道使用Map发送参数的voley 对于前

private void SignInWithEmail() {
        //email= String.valueOf(mEmail.getText());
        //pass = String.valueOf(mPassword.getText());

        String url = RequestUrls.getInstance().signInByEmail();
        StringRequest mRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Log.v(TAG, "Login with email" + response);
                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    String code = jsonResponse.getString("code");
                    if (code == "1") {

                        JSONArray UserDetailArray = jsonResponse.getJSONArray("document");
                        Log.v("Login with email", UserDetailArray.toString());
                        JSONObject finalObject = UserDetailArray.getJSONObject(0);
                        String User_email = finalObject.getString("Email");
                        getUserByEmail(User_email);
                    } else {
                        Toast.makeText(getApplicationContext(), "Invalid Email or Password", Toast.LENGTH_SHORT).show();
                        mPassword.setText("");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.v(TAG, "Request for getUserByEmail Error: " + error.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<String, String>();
                params.put("Action", "GetUserByEmail");
                //Log.v("Login", "Action Email for put: " + email);
                params.put("UserName", email);
                params.put("Password", pass);
                return params;
            }
        };

        VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(mRequest);
    }

但我必须发送 这作为参数怎么做

{
  "jsonrpc": "2.0",
  "method": "signUp",
  "id": "1",
  "params": {
    "email": "abc@gmail.com",
    "fname": "abc",
    "lname": "def",
    "pwd": "123"

  }
}

我必须添加标题或任何其他方法请建议我。我是android开发的新手。

这是从postman chrome extension

拍摄的屏幕

enter image description here

请帮帮我。

4 个答案:

答案 0 :(得分:1)

JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.POST, url, json, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {

    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub

    }
});

你可以使用json参数中的上述代码发送你的json。

答案 1 :(得分:1)

尝试

W/UMSDK: SDK: Task not started: SDK config not loaded

快乐代码

答案 2 :(得分:0)

import java.io.UnsupportedEncodingException;
import java.util.Map;    
import org.json.JSONException;
import org.json.JSONObject;    
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;

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

}

使用CustomRequest并在参数中传递地图。

答案 3 :(得分:0)

将对象/模型/ POJO课程发送到截击请求的步骤。
第1步:确保您的类应该是可解析的 第2步:覆盖模型类中的toString方法 第3步:创建地图,即Map<String, Object> params = new Map<String, Object> (); JSONObject object = new JSONObject(classObject.toString()) params.put("key", object)
第4步:用截击请求输入参数。