-
-
Save weikipeng/6309dc3861ac3e12d931 to your computer and use it in GitHub Desktop.
Revisions
-
tomkoptel revised this gist
Sep 4, 2015 . 2 changed files with 25 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ ```java interface RestApi { @Multipart @Headers({"Accept:text/plain"}) @POST(value = "/j_spring_security_check") Call<com.squareup.okhttp.Response> authenticate( @Part(value = "username") String username, @Part(value = "password") String password @PartMap Map<String, String> params); } ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ ```java // Be careful with ordering otherwise conversion exception can be inevitable Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://some.api") .addConverterFactory(StringConverterFactory.create()); .addConverterFactory(GsonConverterFactory.create()); .build(); RestApi restApi = retrofit.create(RestApi.class); Call<com.squareup.okhttp.Response> call = restApi.authenticate("user", "pass", null); com.squareup.okhttp.Response response = call.execute(); ``` -
tomkoptel revised this gist
Sep 4, 2015 . 1 changed file with 5 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,4 @@ ```java final class StringConverterFactory implements Converter.Factory { private StringConverterFactory() {} @@ -18,7 +8,11 @@ final class StringConverterFactory implements Converter.Factory { @Override public Converter<String> get(Type type) { Class<?> cls = (Class<?>) type; if (String.class.isAssignableFrom(cls)) { return new StringConverter(); } return null; } private static class StringConverter implements Converter<String> { -
tomkoptel renamed this gist
Sep 4, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tomkoptel revised this gist
Sep 4, 2015 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ ```java import com.squareup.okhttp.MediaType; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.ResponseBody; @@ -41,4 +42,5 @@ final class StringConverterFactory implements Converter.Factory { } } } } ``` -
tomkoptel renamed this gist
Sep 4, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tomkoptel created this gist
Sep 4, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ import com.squareup.okhttp.MediaType; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.ResponseBody; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import retrofit.Converter; final class StringConverterFactory implements Converter.Factory { private StringConverterFactory() {} public static StringConverterFactory create() { return new StringConverterFactory(); } @Override public Converter<String> get(Type type) { return new StringConverter(); } private static class StringConverter implements Converter<String> { private static final MediaType PLAIN_TEXT = MediaType.parse("text/plain; charset=UTF-8"); @Override public String fromBody(ResponseBody body) throws IOException { return new String(body.bytes()); } @Override public RequestBody toBody(String value) { return RequestBody.create(PLAIN_TEXT, convertToBytes(value)); } private static byte[] convertToBytes(String string) { try { return string.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } } }