Working on Android Studio and IntelliJ for mobile dev, and when working on a signup page, after sending the request I get this error
com.android.volley.ParseError: org.json.JSONException: Value New of type java.lang.String cannot be converted to JSONObject
but the issue is that i can see on the backend that it is saving the user and giving a 200 OK response
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
This is what my AS frontend code looks like.
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);
}
}
I tried rearranging where the JSON Object is supposed to be, i made it before to where it was inside of the signupRequest() function
you can see
this void will be taking data as json and the response which your server prints is not a json format as i can see it prints
you can change your Server Side response format to a json like
Hope It Helps!