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

标签 javascript java android server retrofit

我刚刚学习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/

相关文章:

javascript - Backbone - 覆盖哈希更改历史

java - Glassfish-5 中的 Set-Cookie header : folding several cookies in one header not working on Firefox (https + HTTP/2. 0)

java - 使用静态实例化方法在构建器类中 Autowiring 依赖项

java - 如何将变量数据从 JFrame 传递到另一个?

java - 在 Samsung Galaxy I7500 Android 手机上进行调试

javascript - 使用无法在手机上正常工作的 anchor 标记从页面重定向到其他页面的特定部分

javascript - 使用具有可编辑网站的 iframe

php - jQuery fullCalendar dayClick - 将(日期)转换为 mm/dd/yy

android - 强制应用程序类关闭

android - 来自 .bat 文件的 ADB shell "run-as"未找到 Unix 命令