贴图库Android Post 上传图片

作个记录,直接上代码:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
        .addFormDataPart("deadline", String.valueOf(System.currentTimeMillis() / 1000 + 60))
        .addFormDataPart("Token", "你的相册对应生成的Token")
        .addFormDataPart("file", "test", RequestBody.create(MediaType.parse("image/*"), img)).build();
Request request = new Request.Builder().
        url("http://up.imgapi.com/")
        .post(requestBody)
        .build();
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, IOException e) {
        System.out.println("error:" +e.getMessage());
    }
    @Override
    public void onResponse(Response response) throws IOException {
        System.out.println("onResponse:" + response.code() + ",msg:"+response.message());
        if (response.isSuccessful()) {
            ResponseBody body = response.body();
            if (body != null) {
                System.out.println("success:" + body.string());
            }
        }
    }
});
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页