从改造版本2.0.0 beta1获取字符串

时间:2015-09-13 06:31:13

标签: android retrofit

我想从改装版本2获取json字符串,我不需要从中获取对象。当onResponse方法调用response.body()时,请帮助获取字符串。

public void getLoginResultFromWebservice(int customerID, String user, String pass) {
    String action = "Login";
    long timeMillis = System.currentTimeMillis();
    String key = new String(Hex.encodeHex(DigestUtils.md5(G.HASH_KEY + timeMillis)));
    String customerId = String.valueOf(customerID);
    String username = user;
    String password = pass;
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(G.URL_Webservice_Base)
            .client(U.getClient())
            .build();
    Webservice webservice = retrofit.create(Webservice.class);
    Call<String> call = webservice.getLoginResult(action,key,customerId,username,password);
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Response<String> response) {
            U.log(" ActivityLogin : OnResponse ");
            U.log(" ActivityLogin : Result " + response.body().toString());
        }

        @Override
        public void onFailure(Throwable t) {
            U.log("ActivityLogin On Failure");
        }
    });
}

修改 我找到了解决方案:

 new Retrofit.Builder()
    .baseUrl(G.URL_Webservice_Base)
    // add a converter for String
    .addConverter(String.class, new ToStringConverter()) // add this
    //.addConverterFactory(GsonConverterFactory.create()) // remove this line
    .build()
    .create(Webservice.class);

ToStringConverter.class

public final class ToStringConverter implements Converter<String> {

@Override
public String fromBody(ResponseBody body) throws IOException {
    return body.string();
}

@Override
public RequestBody toBody(String value) {
    return RequestBody.create(MediaType.parse("text/plain"), value);
}
}

1 个答案:

答案 0 :(得分:0)

你可以尝试

public ApiService getBase64StrService() {
    if (retrofit == null) {
        retrofit = new Retrofit
                .Builder()
                .baseUrl(endpoint)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverter(String.class, new Base64StringConverter())
                .build();
    }
    if (service == null) {
        service = retrofit.create(ApiService.class);
    }

    return service;
}



static class Base64StringConverter implements Converter {

    @Override
    public Object fromBody(ResponseBody body) throws IOException {
        InputStream reader = null;
        try {
            reader = body.byteStream();
            ByteArrayOutputStream writer = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 8];
            int size = -1;
            while ((size = reader.read(buffer)) > 0) {
                writer.write(buffer, 0, size);
            }
            String result = new String(writer.toByteArray());
            return result;
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public RequestBody toBody(Object value) {
        return null;
    }
}