【问题标题】:Can't send JSON String to server in API 25无法在 API 25 中将 JSON 字符串发送到服务器
【发布时间】:2017-06-07 04:23:26
【问题描述】:

嘿,我想将 JSON 字符串发送到服务器并在 JSON 数组中获取响应,

请帮我弄清楚用什么来发送 json 数组并以 json 格式返回响应,我的代码是

public class Welcome extends AppCompatActivity {
    private static final String LOGIN_REQUEST_URL = "http://demo4u.org/leaveapp/ws/login.php";
    JSONObject request = new JSONObject();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        final EditText user_email = (EditText) findViewById(R.id.username);
        final EditText user_password = (EditText) findViewById(R.id.password);
        final Button signin = (Button) findViewById(R.id.signin);
        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String uname = user_email.getText().toString();
                final String pass = user_password.getText().toString();
                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            try {
                                JSONObject jsonresponse = new JSONObject(response);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            try {
                                request.put("user_email", uname);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            try {
                                request.put("user_password", pass);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            final TextView show = (TextView) findViewById(R.id.res);
                            String jsonstring = request.toString();
                            JsonObjectRequest jsonobjreq = new JsonObjectRequest(
                                    Method.POST, LOGIN_REQUEST_URL, jsonstring,
                                    new Response.Listener<JSONObject>() {
                                        @Override
                                        public void onResponse(JSONObject response) {
                                            //Log.d(Tag, toString());
                                            Toast.makeText(Welcome.this,"Volley Error",Toast.LENGTH_LONG).show();
                                            show.setText(response.toString());
                                        }
                                    }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    //Toast.makeText(Welcome.this,"Volley Error",Toast.LENGTH_LONG).show();
                                    VolleyLog.d(Tag, "Error : " + error.getMessage());
                                }
                            }) {
                                @Override
                                public Map<String, String> getHeaders() throws AuthFailureError {
                                    HashMap<String, String> headers = new HashMap<String, String>();
                                    headers.put("Content-Type", "application/json; charset=utf-8");
                                    return headers;
                                }


                            };

                        } catch (JSONException e) {
                        }

请写出我必须用上面的代码替换的代码。

【问题讨论】:

  • 这里有什么异常吗?请发布堆栈跟踪
  • 你能把你的邮件发给我,这样我就可以转发我的代码,因为我现在真的很沮丧……或者通过 yashkumaratri@gmail.com 联系我

标签: java android json android-studio


【解决方案1】:

您可以传递您的 uname 并传递给此方法并取回 jsonString。

private String test(String uname, String pass) {
    try {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user_email", uname);
        jsonObject.put("user_password", pass);
        jsonArray.put(jsonObject);
        return jsonArray.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

【讨论】:

    【解决方案2】:

    如果你得到 JSONArray 作为回报,那么你的

     new Response.Listener<JSONObject>() {
                                        @Override
                                        public void onResponse(JSONObject response) {
                                            //Log.d(Tag, toString());
                                            Toast.makeText(Welcome.this,"Volley Error",Toast.LENGTH_LONG).show();
                                            show.setText(response.toString());
                                        }
                                    }
    

    应该是这样的:

     new Response.Listener<JSONArray>() {
                                        @Override
                                        public void onResponse(JSONArray response) {
                                            //Log.d(Tag, toString());
                                            Toast.makeText(Welcome.this,"Volley Error",Toast.LENGTH_LONG).show();
                                            show.setText(response.toString());
                                        }
                                    }
    

    如果你想发送带有请求的 JSONArray,那么你应该使用 JsonArrayRequest 而不是 JsonObjectRequest

    【讨论】:

    • 这里,他们已经覆盖了getParams方法,在这个地图中你需要添加你的用户名和密码。您可以在第二个块中复制代码,根据需要更改请求类型。 :mobikul.com/parameters-to-volley
    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多