├── LICENSE ├── README.md └── src └── main └── java └── retrofit ├── JsonConverterFactory.java ├── JsonRequestBodyConverter.java └── JsonResponseBodyConverter.java /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sébastien Roch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Retrofit2 JSONObject converter 2 | 3 | A Retrofit 2 converter to convert your request and response data to org.json.JSONObject. 4 | 5 | # Compatibility 6 | 7 | Tested with Retrofit 2.0.0-beta2 8 | 9 | # Usage 10 | 11 | Register the converter: 12 | 13 | ```java 14 | OkHttpClient client = new OkHttpClient(); 15 | Retrofit restAdapter = new Retrofit.Builder() 16 | .baseUrl(endPoint) 17 | .client(client) 18 | .addConverterFactory(JsonConverterFactory.create()) 19 | .build(); 20 | 21 | api = restAdapter.create(PdApiInterface.class); 22 | ``` 23 | 24 | In your API interface file: 25 | 26 | ```java 27 | @POST("/v2/package") 28 | Call subscribePackage(@Body JSONObject data); 29 | 30 | @GET("/v2/account/{id}") 31 | Call getAccount(@Path("id") int id); 32 | ``` 33 | 34 | Then in your code: 35 | 36 | ```java 37 | Call c = api.getAccount(getApiManager().getAccountId()); 38 | c.enqueue(new Callback() { 39 | @Override 40 | public void onResponse(Response response, Retrofit retrofit) { 41 | if (response.isSuccess()) { 42 | // response.body() is a JSONObject 43 | onDataReceived(response.body()); 44 | } 45 | } 46 | 47 | @Override 48 | public void onFailure(Throwable t) { 49 | 50 | } 51 | }); 52 | 53 | ``` -------------------------------------------------------------------------------- /src/main/java/retrofit/JsonConverterFactory.java: -------------------------------------------------------------------------------- 1 | package retrofit; 2 | 3 | import com.squareup.okhttp.RequestBody; 4 | import com.squareup.okhttp.ResponseBody; 5 | import org.json.JSONObject; 6 | import retrofit.Converter; 7 | import java.lang.annotation.Annotation; 8 | import java.lang.reflect.Type; 9 | 10 | public class JsonConverterFactory extends Converter.Factory { 11 | 12 | public static JsonConverterFactory create() { 13 | return new JsonConverterFactory (); 14 | } 15 | 16 | public Converter fromResponseBody(Type type, Annotation[] annotations) { 17 | return new JsonResponseBodyConverter(); 18 | } 19 | 20 | public Converter toRequestBody(Type type, Annotation[] annotations) { 21 | return new JsonRequestBodyConverter(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/retrofit/JsonRequestBodyConverter.java: -------------------------------------------------------------------------------- 1 | package retrofit; 2 | 3 | import com.squareup.okhttp.MediaType; 4 | import com.squareup.okhttp.RequestBody; 5 | import retrofit.Converter; 6 | import java.io.IOException; 7 | 8 | final class JsonRequestBodyConverter implements Converter { 9 | private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8"); 10 | 11 | JsonRequestBodyConverter() { 12 | 13 | } 14 | 15 | public RequestBody convert(T value) throws IOException { 16 | return RequestBody.create(MEDIA_TYPE, value.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/retrofit/JsonResponseBodyConverter.java: -------------------------------------------------------------------------------- 1 | package retrofit; 2 | 3 | import com.squareup.okhttp.ResponseBody; 4 | import org.json.JSONException; 5 | import org.json.JSONObject; 6 | import retrofit.Converter; 7 | import java.io.IOException; 8 | 9 | final class JsonResponseBodyConverter implements Converter { 10 | 11 | JsonResponseBodyConverter() { 12 | 13 | } 14 | 15 | @Override 16 | public T convert(ResponseBody value) throws IOException { 17 | JSONObject jsonObj; 18 | try { 19 | jsonObj = new JSONObject(value.string()); 20 | return (T) jsonObj; 21 | } catch(JSONException e) { 22 | return null; 23 | } 24 | } 25 | } 26 | --------------------------------------------------------------------------------