-
-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0a9d79
commit 19d8b22
Showing
2 changed files
with
58 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package rxhttp.wrapper.utils; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
|
||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import rxhttp.Platform; | ||
import rxhttp.wrapper.OkHttpCompat; | ||
import rxhttp.wrapper.callback.IConverter; | ||
import rxhttp.wrapper.entity.ParameterizedTypeImpl; | ||
|
||
/** | ||
* User: ljx | ||
* Date: 2022/10/30 | ||
* Time: 14:44 | ||
*/ | ||
public class Converter { | ||
|
||
public static <T> T convertTo(Response response, Type rawType, Type... types) throws IOException { | ||
return convert(response, ParameterizedTypeImpl.get(rawType, types)); | ||
} | ||
|
||
public static <T> T convertToParameterized(Response response, Type rawType, Type... actualTypes) throws IOException { | ||
return convert(response, ParameterizedTypeImpl.getParameterized(rawType, actualTypes)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> T convert(Response response, Type type) throws IOException { | ||
ResponseBody body = OkHttpCompat.throwIfFail(response); | ||
LogUtil.log(response, null); | ||
if (type == ResponseBody.class) { | ||
try { | ||
return (T) OkHttpCompat.buffer(body); | ||
} finally { | ||
body.close(); | ||
} | ||
} else if (Platform.get().isAndroid() && type == Bitmap.class) { | ||
return (T) BitmapFactory.decodeStream(body.byteStream()); | ||
} else { | ||
boolean needDecodeResult = OkHttpCompat.needDecodeResult(response); | ||
IConverter converter = OkHttpCompat.request(response).tag(IConverter.class); | ||
return converter.convert(body, type, needDecodeResult); | ||
} | ||
} | ||
} |
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