2

I am trying to a call a rest end point I made for my unity game, but any time I send the users json details via unity to my Springboot application through a Rest call i get the error ...

2018-03-17 02:20:40 [thread] WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver -Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.PushbackInputStream@69ce261; line: 1, column: 2]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.PushbackInputStream@69ce261; line: 1, column: 2]

This it my Unity code

public IEnumerator CallLogin(string Username, string Password) {


    UnityWebRequest www = 
  UnityWebRequest.Post("http://localhost:8080/unity/login", new 
     User(Username, Password).ToJson()); 

    www.uploadHandler.contentType = "application/json";

    yield return www.Send();

    if (www.error != null)
    {

        Debug.Log("Error " + www.error);

    }
    else {

        Debug.Log("Response  " + www.downloadHandler.text);

    }

}

And this is my Springboot java code

@ResponseBody
@RequestMapping(value = "/login", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserModel> Login(@RequestBody User user){

    // check if the username and password provided matches a user in the database.
    String verifiedResponse = this.defaultUserDAOService.verification(user);

    UserModel userModel = new UserModel();

    if(verifiedResponse.equalsIgnoreCase("Verified")){ // if the user was verified successfully (Found in the database)

        userModel.setUser(defaultUserDAOService.getByUsername(user.getUserName()))
                .setSuccessful(true)
                .setResponseMessage("Successful");


        return new ResponseEntity<>(userModel, HttpStatus.OK); // return response to client.


    }else{

        userModel.setUser(new User())
                .setSuccessful(false)
                .setResponseMessage("Username or Password is incorrect");

        return new ResponseEntity<>(userModel, HttpStatus.NOT_FOUND); // return response to client.

    }


}

Finally the User class which has the ToJson function

[Serializable] public class User : MonoBehaviour {

public User(string userName, string password)
{
    this.userName = userName;

    this.password = password;
}

public string userName;

public string password;


public string ToJson()
{
    return JsonUtility.ToJson(this);
}

}

I need help please, i have been battling this for hours.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Eshiett
  • 51
  • 8
  • See this part of your code `User(Username, Password).ToJson()`. Where is the `ToJson()` function? – Programmer Mar 17 '18 at 01:33
  • @Programmer it in the User class, i updated my post, the last code at the bottom of my question is the user class code, you will see the ToJson() method there, thanks. – Eshiett Mar 17 '18 at 01:42
  • Encode the json with `UTF8Encoding` before sending. See the json section from my [other](https://stackoverflow.com/questions/46003824/sending-http-requests-in-c-sharp-with-unity/46008025#46008025) post. Let me know if that fixes your issue. – Programmer Mar 17 '18 at 01:47
  • @Programmer Alrighty, I am on it. – Eshiett Mar 17 '18 at 01:58
  • @Programmer woow, it actually worked. Thanks alot. I would love to ask you about the UTF8Encoding, but I will go and read it and not bug you. Thanks alot. – Eshiett Mar 17 '18 at 02:05
  • It's used to escape json so that some of its values are not considered as syntax when sending. You are welcome! – Programmer Mar 17 '18 at 02:13
  • 1
    @Programmer lol...thanks you very much sir, I appreciate. – Eshiett Mar 17 '18 at 07:27
  • @Programmer please sir are you on skype ? – Eshiett Mar 18 '18 at 13:09
  • I don't skype but you can always create new question here. You don't have to call people sir on this site, – Programmer Mar 18 '18 at 19:57

0 Answers0