有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将OkHttpClient转换为改装版2

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

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

第二部分

            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) 个答案