[英]Uploading compress image to server using retrofit
I am using retrofit in my project. 我在我的项目中使用改造。 Now I need to upload an image on server using retrofit.
现在我需要使用改造在服务器上上传图像。 So I need help in the following question:
所以我在以下问题中需要帮助:
How to upload compressed bitmap to a server using retrofit in form data? 如何使用表单数据中的改进将压缩位图上传到服务器? Any link or example will be helpful.
任何链接或示例都会有所帮助。
Upload can be done using below steps 可以使用以下步骤完成上传
Step 1: Create a method with below code 第1步:使用以下代码创建方法
UploadPhotoRetroService service = ServiceGenerator.createService(MyActivity.class, "base-url");
TypedFile typedFile = new TypedFile("image/jpeg", new File(imagePath));
service.upload(typedFile, new Callback<String>() {
@Override
public void success(String result, Response response) {
// success call back
}
@Override
public void failure(RetrofitError error) {
error.printStackTrace();
}
});
Step 2: Create Interface as below 第2步:创建界面,如下所示
public interface UploadPhotoRetroService {
@Multipart
@POST("/whatever-your-api")
void upload(@Part("Photo") TypedFile file, Callback<String> callback);
}
Step 3: Create class as below 第3步:创建类,如下所示
public class ServiceGenerator {
private ServiceGenerator() {
}
public static <S> S createService(Class<S> serviceClass, String baseUrl) {
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(baseUrl)
.setClient(new OkClient(new OkHttpClient()));
RestAdapter adapter = builder.build();
return adapter.create(serviceClass);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.