gson

How to make a singleton for retrofit 2?

若如初见. 提交于 2019-12-12 19:22:05
问题 If there exists multiple retrofit call, how can i make a singleton of a retrofit, so that there won't be repeated codes within the class, thereby get rid of unnecessary codes. 回答1: Here's an example, but! Although this might be shiny and easy to use, singletons are evil. Try to avoid using them if possible. One way around it is by using dependency injection instead. Anyway. public class Api { private static Api instance = null; public static final String BASE_URL = "your_base_url"; // Keep

How convert a POJO into a readable string on Android for debugging?

这一生的挚爱 提交于 2019-12-12 17:56:37
问题 Given a the following plain-text JSON object class Chat { String id; String title; List<String> messages; } how to I override toString in order to it be human readable only for debugging ? 回答1: You could serialize it into a JSON string: class Chat { private static final GSON = new GSON(); String id; String title; List<String> messages; public String toString() { return BuildConfig.DEBUG ? GSON.toJson(this) : super.toString(); } } 回答2: Your own solution works but it is not too generic. I would

Different JSON array response

落花浮王杯 提交于 2019-12-12 17:22:08
问题 I have problems parsing two different JSON responses. 1: This is the JSON response I get from a RESTful API: { "gear": [ { "idGear": "1", "name": "Nosilec za kolesa", "year": "2005", "price": "777.0" }, { "idGear": "2", "name": "Stresni nosilci", "year": "1983", "price": "40.0" } ] } 2: This response I get from my testing client. I was added some values to the list and then I used gson.toJson for testing output. [ { "idGear": "1", "name": "lala", "year": 2000, "price": 15.0 }, { "idGear": "2"

ANDROID Volley + Gson (POST)

混江龙づ霸主 提交于 2019-12-12 17:18:33
问题 hello. I want to make a post request using gson. I got the class implemented into android website... http://developer.android.com/intl/pt-br/training/volley/request-custom.html UserRequestHelper.userRequest(Request.Method.POST, EndpointURL.USUARIO, null, new Response.Listener<Usuario>() { @Override public void onResponse(Usuario response) { Toast.makeText(getActivity(), "Cadastro realizado com sucesso!", Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void

GSON throwing “Expected BEGIN_OBJECT but was STRING

丶灬走出姿态 提交于 2019-12-12 16:39:01
问题 This is Json Object [ { "UserId":"demouser1", "Catagories":[ { "CatagoryName":"Entertainment", "Persent":"25" }, { "CatagoryName":"Household", "Persent":"25" }, { "CatagoryName":"Movie", "Persent":"25" }, { "CatagoryName":"Misc", "Persent":"25" } ], "RequestId":null, "ResponseStatus":false, "Token":null } ] Used The Following approach to parse the above Json public class CategoryEntity { private String CatagoryName; private String Persent; public String getCatagoryName() { return CatagoryName

Parse json with mixed child objects via gson and send back as list

落花浮王杯 提交于 2019-12-12 16:21:31
问题 I'm facing a problem related to parsing json which have mixed arrays of child classes, and I need to provide that as java list back to client. Sample JSON: { "status": "OK", "results": [ { "type": "one", "Id": "2170676", "count": "456", "title": "title", "description": "description", "oneMemberOne": "11", "oneMemberTwo": "12", } { "type": "two", "Id": "2170677", "count": "123", "title": "title", "description": "description", "twoMemberOne": "21", "twoMemberTwo": "22", } ] } I created one

Retrofit - java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT

我与影子孤独终老i 提交于 2019-12-12 15:22:34
问题 I am trying to parse my own JSON, but getting JSONSyntaxException , here is how my JSON looks: { "type":"success", "value":[ { "id":1, "title":"Title - 1", "name":{ "first":"First - 1", "last":"Last - 1" }, "hobbies":[ "Writing Code - 1", "Listening Music - 1" ] }, ..... ] } Log says: E/app.retrofit_chucknorries.MainActivity$2: ERROR: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 7 column 12 path $.value[0].name 01-21

GSON with several known classes

谁都会走 提交于 2019-12-12 15:17:13
问题 I have the following json { "file": {"file": "foo.c", "owner": "user123"} "methods": [{"name": "proc1", "value":"val"}, {"name":"proc2","value":"val2"}] etc... } I know that I can do something like class file{ public String file public String owner } class methods{ public String name public String value } and I can either call File file= gson.fromJson(jsonInString, File.class); methods[] array = gson.fromJson(jsonInString, methods[].class); but what do I do if I need to handle a complex json

single key value to Json with Gson

a 夏天 提交于 2019-12-12 14:01:06
问题 I have single key-value pair that I need to convert to Json using Gson. How might I do that? Say I have class MyClass{ String key; String value; ...//bunch of other fields public String singleKeyValueToJson(){ return new Gson(key,value);//how do I do this? } } Notice I am not serializing the whole class: just the two fields. 回答1: why don't you create your json manually instead of using library. public String singleKeyValueToJson(){ JSONObject json=new JSONObject(); json.put(key,value); return

Gson custom deserialization

笑着哭i 提交于 2019-12-12 13:19:56
问题 I'm using Gson to create and parse JSON, but I've faced one problem. In my code I use this field: @Expose private ArrayList<Person> persons = new ArrayList<Person>(); But my JSON is formated like this: persons:{count:"n", data:[...]} Data is an array of persons. Is there any way to convert this JSON into my class using Gson? Can I use a JsonDeserializer? 回答1: You'll need a custom deserializer (http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonDeserializer.html