zoukankan      html  css  js  c++  java
  • rxjava&retrofit请求直接返回string

    1.添加gradle依赖:  
      
    compile com.squareup.retrofit2:converter-scalars:2.0.0'  
    2.更换转换器  
    mRetrofit = Retrofit.Builder()  
                    .baseUrl(HttpAddress.SITE)  
                    .addConverterFactory(ScalarsConverterFactory.create())  
                    .build();  
    3.定义返回类型为string  
      
    new Callback<String>() {  
                @Override  
                public void onResponse(Call<String> call, retrofit2.Response<String> response) {  
                    Log.d("debug", response.body());  
                }  
      
                @Override  
                public void onFailure(Call<String> call, Throwable t) {  
      
                }  
            }  
    注意或者不加转换器但是Call<ResponseBody>类型必须是ResponseBody
    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(HttpMethods.BASE_URL)
    // .addConverterFactory(ScalarsConverterFactory.create())
    .build();
    MovieService service = retrofit.create(MovieService.class);
    Call<ResponseBody> call = service.getTopMovie2(0, 20);
    call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
    if (response.isSuccess()) {
    Log.i(TAG, "response.isSuccess()");
    try {
    Log.i(TAG, "onResponse: " + response.body().string());
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    
    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
    Log.i(TAG, "onFailure: " + t.getMessage());
    }
    });
    
    }
    
    public interface MovieService {
    @GET("top250")
    rx.Observable<HttpResult<List<Subject>>> getTopMovie(@Query("start") int start, @Query("count") int count);
    
    @GET("top250")
    Call<ResponseBody> getTopMovie2(@Query("start") int start, @Query("count") int count);
    }


    举例:
    Call<ResponseBody> call = ApiFactory.getInstance().getApiService().updateItems2(accessToken, "cart", cart_params);
    call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
    if (response.isSuccessful()) {
    LogUtil.i("response.isSuccess()");
    try {
    String responseStr = response.body().string();
    LogUtil.i("onResponse,onResponse=" + responseStr);
    JSONObject jsonObject = new JSONObject(responseStr);
    LogUtil.i("onResponse,msg=" + jsonObject.getString("msg"));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
    LogUtil.i("onResponse,onFailure=" + t.getMessage());
    }
    });
     
  • 相关阅读:
    nginx连接php fastcgi配置
    zabbix企业级监控概述和部署
    zabbix配置文件详解
    zabbix自定义键值原理
    ipvsadm命令
    lvs持久连接
    TCP协议的3次握手与4次挥手
    TCP协议的3次握手与4次挥手
    设计模式-模板模式
    设计模式-模板模式
  • 原文地址:https://www.cnblogs.com/baiyi168/p/10266562.html
Copyright © 2011-2022 走看看