Retrofit 2我目前正在使用,我想在我的服务器上传一些照片.我知道,旧版本使用TypedFile类进行上传.如果我们想要使用进度条,我们应该writeTo在TypedFile类中重写方法.
使用retrofit 2库时是否可以显示进度?
我正在尝试Image从我的Android APP上传到Amazon AWS S3,我需要使用AWS Restful API.
我正在使用Retrofit 2来提出请求.
我的应用程序与Amazon S3成功连接并按预期执行请求,但是当我尝试Image从Bucket查看时,图片无法打开.我下载Image到我的电脑并尝试打开但仍然收到图像已损坏的消息.
让我们看看我的完整代码.
我的Gradle依赖项
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
compile 'net.danlew:android.joda:2.8.2'
Run Code Online (Sandbox Code Playgroud)
这里创建了一个文件并启动了请求
File file = new File(mCurrentPhotoPath);
RequestBody body = RequestBody.create(MediaType.parse("image/jpeg"), file);
uploadImage(body, "photo_name.jpeg");
Run Code Online (Sandbox Code Playgroud)
改造界面
public interface AwsS3 {
@Multipart
@PUT("/{Key}")
Call<String> upload(@Path("Key") String Key,
@Header("Content-Length") long length,
@Header("Accept") String accept,
@Header("Host") String host,
@Header("Date") String date,
@Header("Content-type") String contentType,
@Header("Authorization") String authorization,
@Part("Body") RequestBody …Run Code Online (Sandbox Code Playgroud) 我怎样才能下载一个文件(图像/视频)从我的PHP服务器使用Retrofit2?
我无法在网上找到有关如何进行的任何资源或教程;我发现这篇文章处理了SO上的某个下载错误,但我不太清楚。有人能指出我正确的方向吗?
这是我的代码:
文件下载服务.java
public interface FileDownloadService {
@GET(Constants.UPLOADS_DIRECTORY + "/{filename}")
@Streaming
Call<ResponseBody> downloadRetrofit(@Path("filename") String fileName);
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.java(@Blackbelt的解决方案)
private void downloadFile(String filename) {
FileDownloadService service = ServiceGenerator
.createService(FileDownloadService.class, Constants.SERVER_IP_ADDRESS);
Call<ResponseBody> call = service.downloadRetrofit("db90408a4bb1ee65d3e09d261494a49f.jpg");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(final Response<ResponseBody> response, Retrofit retrofit) {
try {
InputStream is = response.body().byteStream();
FileOutputStream fos = new FileOutputStream(
new File(Environment.getExternalStorageDirectory(), "image.jpg")
);
int read = 0; …Run Code Online (Sandbox Code Playgroud)