gpt4 book ai didi

javascript - 如何为我的应用程序设置改造?

转载 作者:行者123 更新时间:2023-12-03 08:21:53 25 4
gpt4 key购买 nike

我刚刚学习Retrofit和Android开发。我想要做的是将一个相当复杂的 JSON 对象从网站发送到服务器,并能够使用 Retrofit 作为我的 Android 应用程序的 Java 对象来检索它。

基本上是这样的,

网站 JSON --Ajax 调用 --> 服务器 --Retrofit--> Android 应用程序(Java 对象/集合)

哪个服务器最适合设置此功能?还有关于如何实现这一目标的任何好的引用吗?

谢谢

最佳答案

通过改造和 Android,您只需要几件事

Java 模型

public class User {
private String name;
private String password;

public User(String name, String password) {
this.name = name;
this.password = password;
}
//Getters and setters
//...
}

改造界面

public interface APIService {
@FormUrlEncoded
@Headers("Accept: application/json")
@POST("register")
Call<AuthRegister> createUser(
@Field("name") String name,
@Field("password") String password
);
}

改造回调

public class AuthRegister {
@SerializedName("message")
@Expose
private String message;
@SerializedName("errors")
@Expose
private Errors errors;

public String getMessage() {
return message;
}
public Errors getErrors() {
return errors;
}
}

网络客户端

public class NetworkClient {
public static Retrofit retrofit;
/*
This public static method will return Retrofit client
anywhere in the appplication
*/
public static Retrofit getRetrofitClient() {
//If condition to ensure we don't create multiple retrofit instances in a single application
if (retrofit == null) {
//Defining the Retrofit using Builder
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.level(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
// .addInterceptor(interceptor)
.connectTimeout(30, TimeUnit.MINUTES)
.build();

retrofit = new Retrofit.Builder()
.baseUrl(Config.BASE_URL) //This is the only mandatory call on Builder object.
.client(client)
.addConverterFactory(GsonConverterFactory.create()) // Convertor library used to convert response into POJO
.build();
}
return retrofit;
}
}

您要显示响应或保存数据的 Activity 内的调用

private void saveUser(String name, String password){
Retrofit retrofit = NetworkClient.getRetrofitClient();
APIService service = retrofit.create(APIService.class);

Call<AuthRegister> call = service.createUser(name, password);
call.enqueue(new Callback<AuthRegister>() {
@Override
public void onResponse(Call<AuthRegister> call, Response<AuthLogin> response) {
if (response.code() == 200) {
if (response.body().getMessage() != null) {
Toast.makeText(mContext, "Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "Could not save user", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(mContext, "Could not save user", Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Call<AuthRegister> call, Throwable t) {
new PrefManager(mContext).clearUser();
Log.e(TAG, t.toString());
Toast.makeText(mContext, "Could not save user", Toast.LENGTH_SHORT).show();
}
});
}

关于javascript - 如何为我的应用程序设置改造?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com