Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当使用gson转换器时,如何将改装作为一个单独的组件使用?_Java_Android_Rest_Gson_Retrofit - Fatal编程技术网

Java 当使用gson转换器时,如何将改装作为一个单独的组件使用?

Java 当使用gson转换器时,如何将改装作为一个单独的组件使用?,java,android,rest,gson,retrofit,Java,Android,Rest,Gson,Retrofit,从@jake Wharton答案中,您应该只调用restAdapter.create一次,并在每次需要与进行交互时重新使用MyTaskService的同一实例。 这一点我怎么强调都不过分。 您可以使用常规的单例模式,以确保您在任何地方都只使用这些对象的一个实例。依赖项注入框架也可以用来管理这些实例,但是如果您还没有使用它的话,那就有点过头了 这是我的密码 public class MusicApi { private static final String API_URL = "https://

从@jake Wharton答案中,您应该只调用restAdapter.create一次,并在每次需要与进行交互时重新使用MyTaskService的同一实例。 这一点我怎么强调都不过分。 您可以使用常规的单例模式,以确保您在任何地方都只使用这些对象的一个实例。依赖项注入框架也可以用来管理这些实例,但是如果您还没有使用它的话,那就有点过头了

这是我的密码

public class MusicApi {
private static final String API_URL = "https://itunes.apple.com";
private static MusicApiInterface sMusicApiInterface;

public static MusicApiInterface getApi() {
    if (sMusicApiInterface == null) {
        sMusicApiInterface = null;
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(API_URL)
                .build();

        sMusicApiInterface = restAdapter.create(MusicApiInterface.class);
    }
    return sMusicApiInterface;
}

public interface MusicApiInterface {
    @GET("/search?entity=musicVideo")
    NetworkResponse getMusic(@Query("term") String term);

    @GET("/search?entity=musicVideo")
    void getMusic(@Query("term") String term, Callback<NetworkResponse> networkResponseCallback);

    @GET("/search?entity=musicVideo")
    Observable<NetworkResponse> getMusicObservable(@Query("term") String term);
}

这让我每次都必须创建一个新的restadapter。在我的应用程序中,有些请求是并行运行的。这种方法正确吗?

您不必每次都创建它,但在创建RestaAdapter时只需创建一次:

public static MusicApiInterface getApi() {
    if (sMusicApiInterface == null) {
       Gson gson = new GsonBuilder()
           .registerTypeAdapter(DiscussionViewMoreContainer.class, new ExplorerDeserializerJson())
           .create();
       RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(API_URL)
            .setConverter(new GsonConverter(gson))
            .build();
       sMusicApiInterface = restAdapter.create(MusicApiInterface.class);
     }
     return sMusicApiInterface;
}
如果需要注册多个
反序列化器
,只需使用对
类/TypeToken
和自定义
反序列化器的实例多次调用。
registerTypeAdapter
。Gson将根据您调用的改装方法的返回类型调用正确的方法。例如

Gson gson = new GsonBuilder()
           .registerTypeAdapter(DiscussionViewMoreContainer.class, new ExplorerDeserializerJson())
           .registerTypeAdapter(OtherModelClass.class, new OtherModelClassDeserializerJson())
           .registerTypeAdapter(OtherModelClass3.class, new OtherModelClass3DeserializerJson())

下面是Singletone RestAdapter和ApiInterface类的完整代码。如果我们使用RxAndroid,我们也可以使用RxJava2CallAdapterFactory

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import kaj.service.customer.utility.ApplicationData;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by @ShihabMama 20/12/18 5.02 AM :)
 */

public class RestAdapter {

    private static Retrofit retrofit = null;
    private static ApiInterface apiInterface;

    public static ApiInterface getRxClient() {
        if (apiInterface == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(ApplicationData.FINAL_URL)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            apiInterface = retrofit.create(ApiInterface.class);
        }
        return apiInterface;
    }

    public static ApiInterface getApiClient() {
        if (apiInterface == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(ApplicationData.FINAL_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            apiInterface = retrofit.create(ApiInterface.class);
        }
        return apiInterface;
    }

}
API接口类

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

/**
 * Created by @Shihab_Mama on 11/25/2016.
 */
public interface ApiInterface {

    // TODO: 12/20/2018 sample below
    @GET("orderapi/getOrders?")
    Call<OrderListModel> getOrders(
            @Query("accessToken") String accessToken,
            @Query("companyId") String companyId,
            @Query("customerId") int customerId);

    @POST("orderapi/placeOrder")
    Call<PlaceOrderResponseModel> placeOrder(
            @Query("accessToken") String accessToken,
            @Query("companyId") String companyId,
            @Query("branchId") int branchId,
            @Query("customerId") int customerId,
            @Query("orderNo") String orderNo,
            @Query("orderItemList") String orderItemList,
            @Query("discountedTotalBill") String discountedTotalBill,
            @Query("discountedTotalVat") String discountedTotalVat);

}
2.调用;
导入文件2.http.GET;
导入文件2.http.POST;
导入2.http.Query;
/**
*由@Shihab_Mama于2016年11月25日创建。
*/
公共接口{
//TODO:2018年12月20日样本如下
@获取(“orderapi/getOrders?”)
打电话订购(
@查询(“accessToken”)字符串accessToken,
@查询(“companyId”)字符串companyId,
@查询(“customerId”)int customerId);
@POST(“orderapi/placeOrder”)
调用placeOrder(
@查询(“accessToken”)字符串accessToken,
@查询(“companyId”)字符串companyId,
@查询(“branchId”)int branchId,
@查询(“customerId”)int customerId,
@查询(“orderNo”)字符串orderNo,
@查询(“orderItemList”)字符串orderItemList,
@查询(“贴现票据”)字符串贴现票据,
@查询(“discountedTotalVat”)字符串discountedTotalVat);
}

这让我每次都必须创建一个新的restadapter。为什么?@Blackbelt在将rest适配器设置为singleton之后。您将如何将gson转换器设置为RestadWater。您将创建一个新的rest生成器并进行设置吗?我有不同的注册类型适配器类。例如,我有不同的类型适配器集,gson gson=new gson builder()。registerTypeAdapter()(GalleryContainer.class,new ExplorerDeserializerJson()).create();…下次如何注册?您可以多次调用
registerTypeAdapter
。@当我设置为singleton时,它将被创建一次..对吗?您将如何更改?除非您创建新的生成器并引用现有对象,否则当我的请求并行运行时,如果我更改,一切都将工作?如果您需要更改它,您不需要si我仍然不知道问题是单例模式还是Gson
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

/**
 * Created by @Shihab_Mama on 11/25/2016.
 */
public interface ApiInterface {

    // TODO: 12/20/2018 sample below
    @GET("orderapi/getOrders?")
    Call<OrderListModel> getOrders(
            @Query("accessToken") String accessToken,
            @Query("companyId") String companyId,
            @Query("customerId") int customerId);

    @POST("orderapi/placeOrder")
    Call<PlaceOrderResponseModel> placeOrder(
            @Query("accessToken") String accessToken,
            @Query("companyId") String companyId,
            @Query("branchId") int branchId,
            @Query("customerId") int customerId,
            @Query("orderNo") String orderNo,
            @Query("orderItemList") String orderItemList,
            @Query("discountedTotalBill") String discountedTotalBill,
            @Query("discountedTotalVat") String discountedTotalVat);

}