Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999542
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:40:14+00:00 2026-05-20T07:40:14+00:00

Here is my Json returned from the server {ErrorCode:1005,Message:Username does not exist} Here is

  • 0

Here is my Json returned from the server

{"ErrorCode":1005,"Message":"Username does not exist"}

Here is my class for an error

public class ErrorModel {
public int ErrorCode;
public String Message;
}

and here is my conversion code.

public static ErrorModel GetError(String json) {

    Gson gson = new Gson();

    try
    {
        ErrorModel err = gson.fromJson(json, ErrorModel.class);

        return err;
    }
    catch(JsonSyntaxException ex)
    {
        return null;
    }
}

It is always throwing a JsonSyntaxException. Any ideas what could be my problem here?

EDIT: As requested, here is further elaboration.

My backend is an ASP.NET MVC 2 application acting as a rest API. The backend isn’t the problem here, as my actions (and even server errors) all return Json (using the built in JsonResult). Here’s a sample.

[HttpPost]
public JsonResult Authenticate(AuthenticateRequest request)
{
    var authResult = mobileService.Authenticate(request.Username, request.Password, request.AdminPassword);

    switch (authResult.Result)
    {
         //logic omitted for clarity
         default:
            return ExceptionResult(ErrorCode.InvalidCredentials, "Invalid username/password");
            break;
    }

    var user = authResult.User;

    string token = SessionHelper.GenerateToken(user.UserId, user.Username);

    var result = new AuthenticateResult()
    {
        Token = token
    };

    return Json(result, JsonRequestBehavior.DenyGet);
}

The basic logic is to auth the user cretentials and either return an ExceptionModel as json or an AuthenticationResult as json.

Here is my server side Exception Model

public class ExceptionModel
{
    public int ErrorCode { get; set; }
    public string Message { get; set; }

    public ExceptionModel() : this(null)
    {

    }

    public ExceptionModel(Exception exception)
    {
        ErrorCode = 500;
        Message = "An unknown error ocurred";

        if (exception != null)
        {
            if (exception is HttpException)
                ErrorCode = ((HttpException)exception).ErrorCode;

            Message = exception.Message;
        }
    }

    public ExceptionModel(int errorCode, string message)
    {
        ErrorCode = errorCode;
        Message = message;
    }
}

When the above authentication is called with invalid credentials, the error result is returned as expected. The Json returned is the Json above in the question.

On the android side, I first build an object with my key-value pairs.

public static HashMap<String, String> GetAuthenticationModel(String username, String password, String adminPassword, String abbr)
{
    HashMap<String, String> request = new HashMap<String, String>();
    request.put("SiteAbbreviation", abbr);
    request.put("Username", username);
    request.put("Password", password);
    request.put("AdminPassword", adminPassword);

    return request;
}

Then, I send off an http post and return as a string whatever is sent back.

public static String Post(ServiceAction action, Map<String, String> values) throws IOException {
    String serviceUrl = GetServiceUrl(action);

    URL url = new URL(serviceUrl);

    URLConnection connection = url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    String data = GetPairsAsString(values);

    DataOutputStream output = new DataOutputStream(connection.getOutputStream());
    output.writeBytes(data);
    output.flush();
    output.close();

    DataInputStream input = new DataInputStream(connection.getInputStream());

    String line;
    String result = "";
    while (null != ((line = input.readLine())))
    {
        result += line;
    }
    input.close ();

    return result;
}

private static String GetServiceUrl(ServiceAction action)
{
    return "http://192.168.1.5:33333" + action.toString();
}

private static String GetPairsAsString(Map<String, String> values){

    String result = "";
    Iterator<Entry<String, String>> iter = values.entrySet().iterator();

    while(iter.hasNext()){
        Map.Entry<String, String> pairs = (Map.Entry<String, String>)iter.next();

        result += "&" + pairs.getKey() + "=" + pairs.getValue();
    }

    //remove the first &
    return result.substring(1);
}

Then I take that result and pass it into my parser to see if it is an error

public static ErrorModel GetError(String json) {

    Gson gson = new Gson();

    try
    {
        ErrorModel err = gson.fromJson(json, ErrorModel.class);

        return err;
    }
    catch(JsonSyntaxException ex)
    {
        return null;
    }
}

But, JsonSyntaxException is always thrown.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:40:15+00:00Added an answer on May 20, 2026 at 7:40 am

    Might help to know more about the exception, but the same code sample works fine here. I suspect there’s a piece of code you omitted that’s causing the problem (perhaps the creation/retrieval of the JSON string). Here’s a code sample that worked fine for me on Java 1.6 and Gson 1.6:

    import com.google.gson.Gson;
    
    public class ErrorModel {
      public int ErrorCode;
      public String Message;
      public static void main(String[] args) {
        String json = "{\"ErrorCode\":1005,\"Message\":\"Username does not exist\"}";
        Gson gson = new Gson();
        ErrorModel err = gson.fromJson(json, ErrorModel.class);
        System.out.println(err.ErrorCode);
        System.out.println(err.Message);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the JSON string returned from my action: [{Key:Likes,Value:1},{Key:Loves,Value:0},{Key:Dislikes,Value:0},{Key:Message,Value:Your vote has been changed}] Here's
I have JSON returned from my server like: { user: { nickname: Ann, width:
Here is my home-made Serializing class: public class JsonBuilder { private StringBuilder json; public
I'm trying to parse the JSON returned from SocialMention. Here's an example of what
Currently I'm using the helper methods outlined here to return some JSON from my
Why does Google prepend while(1); to their (private) JSON responses? For example, here's a
So i'm trying to de-serialize the JSON returned from the Graph API OAuth Token
I am using jQuery ajax method to get response from my server. Here's my
I am not sure what is wrong with my JSON response from WCF but
This is a strange error and I'm not sure if I'm using JSON correctly.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.