com.android.volley.ParseError:org.json.JSONException:java.lang.String类型的新值无法转换为JSONObject

问题描述 投票:0回答:1

在用于移动开发的 Android Studio 和 IntelliJ 上工作,在注册页面上工作时,发送请求后收到此错误

com.android.volley.ParseError: org.json.JSONException: Value New of type java.lang.String cannot be converted to JSONObject

但问题是我可以在后端看到它正在保存用户并给出 200 OK 响应


POST "/people", parameters={}
Mapped to coms309.people.PeopleController#createPerson(Person)
Read "application/json;charset=utf-8" to [5 dave borucki [email protected] dunk]
5 dave borucki [email protected] dunk
Using 'text/plain', given [*/*] and supported [text/plain, */*, application/json, application/*+json]
Writing ["New person dave Saved"]
Completed 200 OK

这就是我的 AS 前端代码的样子。

package com.example.androidexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class SignupActivity extends AppCompatActivity {
    private EditText firstNameEditText;
    private EditText lastNameEditText;
    private EditText emailAddressEditText;
    private EditText passwordEditText;
    private Button signupButton;
    private Button loginButton;
    private JSONObject user;
    private static final String URL_SIGNUP =  "http://10.0.2.2:8081/people";
    private EditText errorEditText;

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


        /* initialize UI elements */
        firstNameEditText = findViewById(R.id.signup_firstName_edt);  // link to username edtext in the Signup activity XML
        lastNameEditText = findViewById(R.id.signup_lastName_edt);  // link to password edtext in the Signup activity XML
        emailAddressEditText = findViewById(R.id.signup_email_edt);    // link to confirm edtext in the Signup activity XML
        passwordEditText = findViewById(R.id.signup_password_edt);    // link to login button in the Signup activity XML
        signupButton = findViewById(R.id.signup_signup_btn);  // link to signup button in the Signup activity XML
        loginButton = findViewById(R.id.signup_login_btn);
        errorEditText = findViewById(R.id.errorText);

        /* click listener on login button pressed */
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /* when login button is pressed, use intent to switch to Login Activity */
                Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
                startActivity(intent);  // go to LoginActivity
            }
        });

        /* click listener on signup button pressed */
        signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /* grab strings from user inputs */
                String firstName = firstNameEditText.getText().toString();
                String lastName = lastNameEditText.getText().toString();
                String emailAddress = emailAddressEditText.getText().toString();
                String password = passwordEditText.getText().toString();
                System.out.println("Signup pressed");
                user = new JSONObject();
                try {
                    user.put("id", 5);
                    user.put("firstName", firstName);
                    user.put("lastName", lastName);
                    user.put("emailAddress", emailAddress);
                    user.put("password", password);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                signUpRequest();
            }
        });
    }
    private void signUpRequest() {
        RequestQueue queue = Volley.newRequestQueue(this);
        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.POST,
                URL_SIGNUP,
                user,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Toast.makeText(getApplicationContext(),"Account Created", Toast.LENGTH_LONG).show();
                        Intent successIntent = new Intent(SignupActivity.this, MainActivity.class);
                        //successIntent.putExtra("id", id);
                        startActivity(successIntent);
                    }
                },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Signup unsuccessful (VolleyError)", Toast.LENGTH_LONG).show();
                errorEditText.setText(error.toString());
            }
        });
        queue.add(request);
    }
}

我尝试重新排列 JSON 对象应该在的位置,我之前已经将其放置在signupRequest() 函数内部的位置

java android frontend android-volley
1个回答
0
投票

你可以看到

public void onResponse(JSONObject 响应)

这个 void 将以 json 形式获取数据,并且您的服务器打印的响应不是我所看到的 json 格式打印

完成200 OK

您可以将服务器端响应格式更改为 json

{
  "status": 200,
  "message": "COMPLETED OK"
}

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.