简体   繁体   English

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

[英]How to set up retrofit for my application?

I am just learning about Retrofit and Android development.我只是在学习 Retrofit 和 Android 开发。 What I would like to do is send a fairly complex JSON object to a server from a website and be able to retrieve it using Retrofit as a Java Object for my Android Application.我想要做的是将一个相当复杂的 JSON 对象从网站发送到服务器,并能够使用 Retrofit 作为我的 Android 应用程序的 Java 对象来检索它。

So basically something like this,所以基本上是这样的,

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

Which server would be the best to set this up?哪个服务器最适合设置? Also are there any good references on how to accomplish this?还有关于如何实现这一点的任何好的参考资料吗?

Thank you谢谢

With retrofit and android, you only need a couple of things使用改造和安卓,你只需要几件事

A java model一个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
//...
}

A retrofit interface改造界面

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

A callback for retrofit改造回调

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;
    }
}

A network client网络客户端

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;
    }
}

The call inside the activity you are to display the response or save the data您要显示响应或保存数据的活动中的调用

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();
                            }
                        });
}

You can use any server as you need.您可以根据需要使用任何服务器。 Any complex JSON can be handled by retrofit library.任何复杂的 JSON 都可以由改造库处理。 Check the Following link Retrofit android example web services检查以下链接Retrofit android example web services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM