java - Gson - attempting to convert json string to custom object -
here json returned server
{"errorcode":1005,"message":"username not exist"}
here class error
public class errormodel { public int errorcode; public string message; }
and here 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 throwing jsonsyntaxexception. ideas problem here?
edit: requested, here further elaboration.
my backend asp.net mvc 2 application acting rest api. backend isn't problem here, actions (and server errors) return json (using built in jsonresult
). here's sample.
[httppost] public jsonresult authenticate(authenticaterequest request) { var authresult = mobileservice.authenticate(request.username, request.password, request.adminpassword); switch (authresult.result) { //logic omitted 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 auth user cretentials , either return exceptionmodel json or authenticationresult json.
here 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 httpexception) errorcode = ((httpexception)exception).errorcode; message = exception.message; } } public exceptionmodel(int errorcode, string message) { errorcode = errorcode; message = message; } }
when above authentication called invalid credentials, error result returned expected. json returned json above in question.
on android side, first build object 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, send off http post , return string whatever 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 first & return result.substring(1); }
then take result , pass parser see if 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 thrown.
might know more exception, same code sample works fine here. suspect there's piece of code omitted that's causing problem (perhaps creation/retrieval of json string). here's code sample worked fine me on java 1.6 , 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 not exist\"}"; gson gson = new gson(); errormodel err = gson.fromjson(json, errormodel.class); system.out.println(err.errorcode); system.out.println(err.message); } }
Comments
Post a Comment