Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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
Android 从改装版本2.0.0 beta1获取字符串_Android_Retrofit - Fatal编程技术网

Android 从改装版本2.0.0 beta1获取字符串

Android 从改装版本2.0.0 beta1获取字符串,android,retrofit,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

我想从改型版本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");
        }
    });
}
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);
}
}
公共最终类ToString Converter实现转换器{
@凌驾
来自body(ResponseBody body)的公共字符串引发IOException{
返回body.string();
}
@凌驾
公共请求主体到主体(字符串值){
返回RequestBody.create(MediaType.parse(“text/plain”),值);
}
}
您可以试试

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;
    }
}

这不行!
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;
    }
}