-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from SmallPlanetAndroid/master
Logs Improvements
- Loading branch information
Showing
23 changed files
with
366 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
37 changes: 37 additions & 0 deletions
37
...src/main/java/com/learningmachine/android/app/data/webservice/LMGsonConverterFactory.java
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.learningmachine.android.app.data.webservice; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
import okhttp3.RequestBody; | ||
import okhttp3.ResponseBody; | ||
import retrofit2.Converter; | ||
import retrofit2.Retrofit; | ||
|
||
public class LMGsonConverterFactory extends Converter.Factory { | ||
private static final Gson gson = new Gson(); | ||
|
||
public static LMGsonConverterFactory create() { | ||
return new LMGsonConverterFactory(); | ||
} | ||
|
||
@Override | ||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, | ||
Retrofit retrofit) { | ||
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type)); | ||
return new LMGsonResponseBodyConverter<>(gson, adapter); | ||
} | ||
|
||
@Override | ||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, | ||
Annotation[] methodAnnotations, Retrofit retrofit) { | ||
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type)); | ||
return new LMGsonRequestBodyConverter<>(gson, adapter); | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...main/java/com/learningmachine/android/app/data/webservice/LMGsonRequestBodyConverter.java
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.learningmachine.android.app.data.webservice; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.nio.charset.Charset; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okio.Buffer; | ||
import retrofit2.Converter; | ||
|
||
final class LMGsonRequestBodyConverter<T> implements Converter<T, RequestBody> { | ||
private static final MediaType MEDIA_TYPE = MediaType.get("application/json; charset=UTF-8"); | ||
|
||
private final TypeAdapter<T> adapter; | ||
private final Gson gson; | ||
|
||
LMGsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) { | ||
this.gson = gson; | ||
this.adapter = adapter; | ||
} | ||
|
||
@Override | ||
public RequestBody convert(T value) throws IOException { | ||
Buffer buffer = new Buffer(); | ||
Writer writer = new OutputStreamWriter(buffer.outputStream(), Charset.forName("UTF-8")); | ||
JsonWriter jsonWriter = gson.newJsonWriter(writer); | ||
adapter.write(jsonWriter, value); | ||
jsonWriter.close(); | ||
return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); | ||
} | ||
} |
Oops, something went wrong.