Skip to content

Instantly share code, notes, and snippets.

@weikipeng
Forked from tomkoptel/RestApi.md
Last active September 6, 2015 05:14
Show Gist options
  • Select an option

  • Save weikipeng/6309dc3861ac3e12d931 to your computer and use it in GitHub Desktop.

Select an option

Save weikipeng/6309dc3861ac3e12d931 to your computer and use it in GitHub Desktop.

Revisions

  1. @tomkoptel tomkoptel revised this gist Sep 4, 2015. 2 changed files with 25 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions RestApi.md
    Original 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);
    }
    ```
    14 changes: 14 additions & 0 deletions Usage Sample.md
    Original 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();

    ```
  2. @tomkoptel tomkoptel revised this gist Sep 4, 2015. 1 changed file with 5 additions and 11 deletions.
    16 changes: 5 additions & 11 deletions StringConverterFactory.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,4 @@
    ```java
    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() {}

    @@ -18,7 +8,11 @@ final class StringConverterFactory implements Converter.Factory {

    @Override
    public Converter<String> get(Type type) {
    return new StringConverter();
    Class<?> cls = (Class<?>) type;
    if (String.class.isAssignableFrom(cls)) {
    return new StringConverter();
    }
    return null;
    }

    private static class StringConverter implements Converter<String> {
  3. @tomkoptel tomkoptel renamed this gist Sep 4, 2015. 1 changed file with 0 additions and 0 deletions.
  4. @tomkoptel tomkoptel revised this gist Sep 4, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion StringConverterFactory.class
    Original 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 {
    }
    }
    }
    }
    }
    ```
  5. @tomkoptel tomkoptel renamed this gist Sep 4, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @tomkoptel tomkoptel created this gist Sep 4, 2015.
    44 changes: 44 additions & 0 deletions gistfile1.txt
    Original 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);
    }
    }
    }
    }