【问题标题】:How to set up retrofit for my application?如何为我的应用程序设置改造?
【发布时间】:2016-02-16 03:12:19
【问题描述】:

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

所以基本上是这样的,

Website JSON --Ajax Call--> Server --Retrofit--> Android Application (Java Object / Collection)

哪台服务器最适合设置这个?还有关于如何做到这一点的任何好的参考资料吗?

谢谢

【问题讨论】:

    标签: javascript java android server retrofit


    【解决方案1】:

    您可以根据需要使用任何服务器。改造库可以处理任何复杂的 JSON。检查以下链接Retrofit android example web services

    【讨论】:

      【解决方案2】:

      使用改造和 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();
                                  }
                              });
      }
      
      

      【讨论】:

        猜你喜欢
        • 2011-12-11
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多