gpt4 book ai didi

android - 使用 volley 获取响应 200 将嵌套的 Json 对象发布到服务器

转载 作者:行者123 更新时间:2023-11-29 01:22:38 25 4
gpt4 key购买 nike

  • 我在使用 post response api 时收到错误响应volley.下面我贴出了json格式的。

  • 而且我还发布了我目前在 mainactivity.java 中尝试过的内容。

  • 我不知道我做错了什么。但我收到了错误的回复简单地说。我提到了这些 Sample做一个带有内部 json 对象响应的帖子到服务器。

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

}
}

日志:

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

最佳答案

作为我的评论,您可以引用我对以下问题的回答中的示例代码(注意 getBody()。如果您的网络服务的响应是 JSONObject,你可以使用JsonObjectRequest代替StringRequest)

How to send a POST request using volley with string body?

关于android - 使用 volley 获取响应 200 将嵌套的 Json 对象发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35895134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com