android – get String来自Retrofit版本2.0.0 beta1

android – get String来自Retrofit版本2.0.0 beta1,第1张

概述我想从改装版本2获得json字符串,我不需要从中获取对象.当onResponse方法调用response.body()时,请帮助获取字符串. public void getLoginResultFromWebservice(int customerID, String user, String pass) { String action = "Login"; long timeMi 我想从改装版本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> {@OverrIDepublic String fromBody(ResponseBody body) throws IOException {    return body.string();}@OverrIDepublic Requestbody toBody(String value) {    return Requestbody.create(MediaType.parse("text/plain"),value);}}
解决方法 你可以试试

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,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;    }}
总结

以上是内存溢出为你收集整理的android – get String来自Retrofit版本2.0.0 beta1全部内容,希望文章能够帮你解决android – get String来自Retrofit版本2.0.0 beta1所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123592.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存