update 优化下写法
This commit is contained in:
parent
0d65df5271
commit
be685a3745
@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.yutou.netlibs.http.HttpBody;
|
||||
import com.yutou.netlibs.http.HttpCallback;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -21,25 +22,25 @@ public class MainActivity extends AppCompatActivity {
|
||||
findViewById(R.id.button)
|
||||
.setOnClickListener(v -> {
|
||||
Api.getInstance().getList("")
|
||||
.enqueue(new Callback<HttpBody<List<Password>>>() {
|
||||
.enqueue(new HttpCallback<List<Password>>() {
|
||||
@Override
|
||||
public void onResponse(Call<HttpBody<List<Password>>> call, Response<HttpBody<List<Password>>> response) {
|
||||
System.out.println("response = " + response.body().getData());
|
||||
public void onResponse(int code, String status, List<Password> response) {
|
||||
System.out.println("code = " + code + ", status = " + status + ", response = " + response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<HttpBody<List<Password>>> call, Throwable t) {
|
||||
public void onFailure(Throwable throwable) {
|
||||
|
||||
}
|
||||
});
|
||||
Api.getInstance().getVersion().enqueue(new Callback<HttpBody<Bean>>() {
|
||||
Api.getInstance().getVersion().enqueue(new HttpCallback<Bean>() {
|
||||
@Override
|
||||
public void onResponse(Call<HttpBody<Bean>> call, Response<HttpBody<Bean>> response) {
|
||||
System.out.println("response = " + response.body().getData());
|
||||
public void onResponse(int code, String status, Bean response) {
|
||||
System.out.println("code = " + code + ", status = " + status + ", response = " + response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<HttpBody<Bean>> call, Throwable t) {
|
||||
public void onFailure(Throwable throwable) {
|
||||
|
||||
}
|
||||
});
|
||||
|
@ -25,6 +25,6 @@ public class JsonConverterFactory extends Converter.Factory {
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
// return super.responseBodyConverter(type, annotations, retrofit);
|
||||
return new JsonResponseBodyConverter<>();
|
||||
return new JsonResponseBodyConverter<>(type);
|
||||
}
|
||||
}
|
||||
|
@ -8,16 +8,22 @@ import org.jetbrains.annotations.Nullable;
|
||||
import retrofit2.Converter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
|
||||
Type type;
|
||||
public JsonResponseBodyConverter(Type type) {
|
||||
this.type=type;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public T convert(ResponseBody responseBody) throws IOException {
|
||||
String string = new String(responseBody.bytes());
|
||||
responseBody.close();
|
||||
HttpBody body ;
|
||||
HttpBody<T> body ;
|
||||
try {
|
||||
body = JSONObject.parseObject(string, HttpBody.class);
|
||||
body = JSONObject.parseObject(string, type);
|
||||
return (T) body;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.yutou.netlibs.http;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public abstract class HttpCallback<T> implements Callback<HttpBody<T>> {
|
||||
|
||||
public abstract void onResponse(int code,String status,T response);
|
||||
public abstract void onFailure(Throwable throwable);
|
||||
|
||||
@Override
|
||||
public void onResponse(Call<HttpBody<T>> call, Response<HttpBody<T>> response) {
|
||||
if (response.body() != null) {
|
||||
System.out.println("response = " + response.body());
|
||||
System.out.println("response.data = " + response.body().getData());
|
||||
onResponse(response.body().getCode(),response.body().getMsg(),response.body().getData());
|
||||
}else{
|
||||
onFailure(new NullPointerException("response body is null"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<HttpBody<T>> call, Throwable throwable) {
|
||||
onFailure(throwable);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user