简体   繁体   English

将改造添加到android项目中

[英]Adding retrofit into android project

I want to try using Retrofit in a new Android project.我想尝试在新的 Android 项目中使用 Retrofit。

I have added the following to my build.gradle:我在 build.gradle 中添加了以下内容:

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.google.code.gson:gson:2.8.0'

I have a POJO called 'Turbine' which looks as follows:我有一个名为“Turbine”的 POJO,如下所示:

public class Turbine {
    String name;
}

I have my Endpoint service class:我有我的端点服务类:

import java.util.List;

import greenapps.objects.Turbine;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

    public interface GreenAppService {
        @GET("turbines/{id}")
        Call<List<Turbine>> turbine(@Path("id") String id);
    }

In my main activity in android I have the following code (this is where I want to execute the call and get back my Turbine pojo object filled with data from the backend:在我在 android 中的主要活动中,我有以下代码(这是我想要执行调用并从后端取回充满数据的 Turbine pojo 对象的地方:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
...
...
...
//Relevant snippet starts here
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_V1_ENDPOINT)
            .build();

    GreenAppService service = retrofit.create(GreenAppService.class);
    Call<List<Turbine>> turbine = service.turbine("1");
    turbine.enqueue(new Callback<Turbine>() {
        @Override
        public void onResponse(Call<Turbine> call, Response<Turbine> response) {
            
        }

        @Override
        public void onFailure(Call<Turbine> call, Throwable t) {

        }
});//Relevant snippet ends here

On the turbine.enqueue line I am getting the following error:turbine.enqueue行上,我收到以下错误:

在此处输入图片说明

I presume the syntax here is wrong somehow, but I don't quite see what is causing the issue.我认为这里的语法不知何故是错误的,但我不太明白是什么导致了这个问题。

Also, once this works, how do I get my Turbine object?另外,一旦这有效,我如何获得我的 Turbine 对象? Is it a case of doing Turbine t = response.body();是不是做Turbine t = response.body();

Because you defined that you are waiting an Array of Turbine.因为你定义了你正在等待一个涡轮机阵列。

turbine.enqueue(new Callback<Turbine>()

to

turbine.enqueue(new Callback<ArrayList<Turbine>>()

also you need to update onResponse and onFailure methods with arraylist.您还需要使用 arraylist 更新onResponseonFailure方法。

I strongly suggest you to apply singleton pattern for your GreenAppService object.我强烈建议您为GreenAppService对象应用单例模式。

UPDATE更新

Here is an example of singleton pattern.这是单例模式的示例。

public final class WebService {

   private static GreenAppService sInstance;

   public static GreenAppService getInstance() {
       if (sInstance == null) {
           sInstance = new Retrofit
           .Builder()
           .baseUrl(Constants.API_V1_ENDPOINT)
           .build();
       }

       return sInstance;
   }
}

After that we call like之后我们称之为

WebService
    .getInstance()
    .yourmethod()
    .enqueue()

Easy way to add retrofit in your project with just one click.只需单击一下,即可轻松在项目中添加改造。 (one-time setup) (一次性设置)

Retrofit zip 改装拉链

  1. Extract the file and you will get the “Retrofit” folder inside.解压缩文件,您将在其中获得“改造”文件夹。

  2. Copy this folder following the Android Studio path Android\\Android Studio\\plugins\\android\\lib\\templates\\other按照 Android Studio 路径 Android\\Android Studio\\plugins\\android\\lib\\templates\\other 复制此文件夹

  3. Restart your Android Studio.重启你的 Android Studio。

  4. Select your project in which you want to add retrofit and find an option below.选择您要添加改造的项目,然后在下面找到一个选项。

Project > New > Other> Retrofit项目 > 新建 > 其他 > 改造

You can see full blog here你可以在这里看到完整的博客

https://medium.com/@mestri.vinayak.n/quick-install-retrofit-in-your-android-project-custom-template-a14a6adc77c2 https://medium.com/@mestri.vinayak.n/quick-install-retrofit-in-your-android-project-custom-template-a14a6adc77c2

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

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