将OkHttpClient转换为Retrofit 2

时间:2018-02-14 17:37:58

标签: java android retrofit2 okhttp3

我使用第1部分代码调用成功运行的API,但我想用Retrofit2实现逻辑。我在第2部分中已经完成了实现,但代码总是带有响应FORBIDDEN。

URL将文本文件作为字节数组进行上传。任何人都可以查看代码并指导我做错了什么

第1部分

public void  testcall(byte[] bytesArray) {
                OkHttpClient client = new OkHttpClient();
                RequestBody formBody = RequestBody.create(MediaType.parse("application/json") , bytesArray);
                Request request = new Request.Builder()
                        .url("https://logs-01.loggly.com/bulk/Token/tag/file_uploadTest")
                        .post(formBody)
                        .build();
Response response = client.newCall(request).execute();
}

第2部分

            public interface ApplicationLog {
             @POST("/")
                LogResponse uploadLog(@Body RequestBody body);
            }

public class ApplicationLogSender {

    private String url;

    public ApplicationLogSender(String url) {
        this.url = url;

    }

    public ApplicationLog   applicationLogSenderBuilder() {
        System.out.println("Log url" + url);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        return retrofit.create(ApplicationLog.class);
    }
}

        public void  testcall(byte[] bytesArray) {
           RequestBody formBody = RequestBody.create(MediaType.parse("application/json") , bytesArray);
    // I also tried with "Text/plain but no success"

        LogResponse = builder.uploadLog(formBody).enqueue // This line always throw Forbidden
        }

0 个答案:

没有答案