使用截击将嵌套的Json对象发布到服务器上以获取响应200 [英] Post nested Json Object to server using volley getting response 200

查看:71
本文介绍了使用截击将嵌套的Json对象发布到服务器上以获取响应200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 使用 截击.下面我发布了json格式.

  • I am getting false response while hitting the post response api using volley.Below I have posted the json format.

我也将我到目前为止尝试过的内容发布到了mainactivity.java中.

And also I have posted what I had tried so far in mainactivity.java.

我不知道我做错了什么,但是我得到了错误的答复 简单地.我提到了这些 示例 用内部json对象响应对服务器进行发布.

I don't know What I did wrong.But i am getting a false response simply.I referred these Sample to do a post with inner json object response to server.

Json格式:(已编辑)

    {
"facebookData":{
      "about" : "",
    "access_token":"",
    "age":28,
    "birthday":"563221800",
    "email":"mailtosteve3@gmail.com",
    "facebook_user_id":"561394210664929",
    "first_name":"Bradley",
    "gender":"male",
    "id":"561394210664929",
    "is_show_only":"Men and Women",
    "latitude":"13.05505200",
    "longitude":"80.23623600",
    "name":"Bradley Cummings",
    "profilePicture":"",
    "provider":"Facebook"
}
}

MainActivity.java:(已编辑)

  private void validateUser() {


        final ProgressDialog dialog = ProgressDialog.show(FacebookActivity.this, null, null);
        ProgressBar spinner = new android.widget.ProgressBar(FacebookActivity.this, null, android.R.attr.progressBarStyle);
        spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(spinner);
        dialog.setCancelable(false);
        dialog.show();

        try {

            RequestQueue queue = Volley.newRequestQueue(FacebookActivity.this);

            String userValidationURL = BurblrUtils.BR_FB;

            Log.e("userValidationURL", userValidationURL);


            jsonWholeObject = new JSONObject();
            JSONObject jsonFaceBook = new JSONObject();


            jsonFaceBook.put("about", "Note");
            jsonFaceBook.put("access_token", facebookAccessToken);
            jsonFaceBook.put("age", "28");
            jsonFaceBook.put("birthday", "563221800");
            jsonFaceBook.put("email", emailString);
            jsonFaceBook.put("facebook_user_id", faceBookId);
            jsonFaceBook.put("first_name", firstString);
            jsonFaceBook.put("gender", genderString);
            jsonFaceBook.put("id", id);
            jsonFaceBook.put("is_show_only", "Men and Women");
            jsonFaceBook.put("latitude", "13.05505200");
            jsonFaceBook.put("longitude", "80.23623600");
            jsonFaceBook.put("name", firstString + lastString);
            jsonFaceBook.put("profilePicture", profile_pic);
            jsonFaceBook.put("provider", "Facebook");

            jsonWholeObject.put("facebookData", jsonFaceBook);

            mRequestBody = jsonWholeObject.toString();

            StringRequest request = new StringRequest(Request.Method.POST, userValidationURL, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    Log.e("FaceBookRes", response);

                    if (response != null && !response.startsWith("<HTML>")) {

                        Log.e("onResponse", "" + response);
                        dialog.dismiss();

                        try {


                            JSONObject login_obj = new JSONObject(response);

                            String message = login_obj.getString("message");
                            String error = login_obj.getString("error");

                            if (message.equals("Facebook Connection problem")) {

                                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

                            } else {

                                Log.e("Else...", "Else...");

                                Intent intent = new Intent(FacebookActivity.this, HomeActivity.class);
                                startActivity(intent);
                                finish();


                            }
                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

                    } else {
                        dialog.dismiss();

                        Toast.makeText(getApplicationContext(), "Internet Toast Message", Toast.LENGTH_SHORT).show();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error != null) {
                        Log.e("error", error.toString());
                        dialog.dismiss();
                    }
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }

                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {

                        mRequestBody = jsonWholeObject.toString();

                        return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                                mRequestBody, "utf-8");
                        return null;
                    }
                }

                @Override
                protected Response<String> parseNetworkResponse(NetworkResponse response) {
                    String responseString = "";
                    if (response != null) {
                        responseString = String.valueOf(response.statusCode);
                        // can get more details such as response.headers
                    }
                    return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
                }

            };


            queue.add(request);

        } catch (JSONException je) {

            je.printStackTrace();

        }
      }

Logcat:

04-01 06:45:15.606 12667-12667/? E/FaceBookRes: 200
04-01 06:45:15.606 12667-12667/? E/onResponse: 200

推荐答案

作为我的评论,您可以在以下问题的答案中引用示例代码(请注意getBody().如果您的Web服务响应是JSONObject,则可以使用JsonObjectRequest代替StringRequest)

As my comments, you can refer to the sample code at my answer at the following question (pay attention to getBody(). If the response from your web service is a JSONObject, you can use JsonObjectRequest instead of StringRequest)

如何发送POST请求用排球打排?

这篇关于使用截击将嵌套的Json对象发布到服务器上以获取响应200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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