android - How can I return String or JSONObject from asynchronous callback using Retrofit? -


After the

For example, calling

  api.getUserName (userId, new callback & lt; String & gt; () {...});  

Reason:

  retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: one of hope The string was but the BEGIN_OBJECT line 1 on the column 2  

I think I have to disable the gson to pars in POJOs, but it can not know how to do it.

I understood it is shameful, but it is very simple ... temporary solution This could have been done:

  Public zero breakthrough (feedback, feedback, ignored) {TypedInput body = response. GetBody (); Try {BufferedReader Reader = New BufferedReader (New InputStreamReader (body.in ()); Stringbuilder = new string builder (outside); String newline = System.getProperty ("line.separator"); String line; While ((line = reader. Readline ()) = null (); out.append (line); Out.append (newline); } / / Println print out the correct string of body (outside); } Hold (IOException e) {e.printStackTrace (); }}  

But if you want to get a direct callback, use the better method . Public class major {public interface API service {@GET ("/ API /") public zero received Jason (callback callback); } Public static void (String [] args) {RestAdapter restAdapter = New RestAdapter.Builder () .setClient (New MockClient ()) .setConverter (New StringConverter ()) .setEndpoint ("http://www.example.com" ) .build (); ApiService service = restAdapter.create (ApiService.class); Service.getJson (new callback & lt; string & gt; () {@Override public zero breakthrough (string str, ignored response) {// println (STR) prints the correct string representation of body; Override Public Zero Failure (Retroirror Retrofit Errors) {System.out.println ("Failure, RetroTerror Error" + RetroTit Error);}}); } Static class string converter converter {@Override Public Object Sebody (typed input type input, type type) conversion upstream {string text = empty throws; Try {stream = stream (type input.in ()); } Grip (IOException ignored) {/ * NOP return / return text; } @ Override public typed output to body (object o) {return tap; } Publicly throws string string input (InputStream) IOException {BufferedReader Reader = New BufferedReader (new InputStreamReader (in)); Stringbuilder = new string builder (outside); String newline = System.getProperty ("line.separator"); String line; While ((line = reader. Readline ()) = null (); out.append (line); Out.append (newline); } For return. String (); }} Public Static Class Mockclant Tool Clients {@Override Public Response Performance (Request Request) throws IOException {URI Uuri = URI.create (request.getUrl ()); String response string = ""; If (uri.getPath (.) Equals ("/ api /")) {responseString = "{Result: \" OK \ "}"; } And {responseString = "{Result: \" error \ "}"; } New Response (request.getUrl (), 200, "Nothing", Collection .EMPTY_LIST, New typed characters ("App / JS", Reactions strings .getBytes ()) Return;}}}

If you know how to improve this code - please do not hesitate to write about it.


Comments