Android 使用改型将文件上载到服务器,其中内容用JSON包装
我想重写上传文件到服务器使用改装 主体的服务器api请求为Android 使用改型将文件上载到服务器,其中内容用JSON包装,android,retrofit2,Android,Retrofit2,我想重写上传文件到服务器使用改装 主体的服务器api请求为 {“file_name”: “my_pic”, “content_base64”: “MKMD….”} 在上传之前,还需要压缩图像并对内容进行编码。我们目前的实施是: Bitmap bmp = BitmapFactory.decodeFile(localPath); ByteArrayOutputStream outputStream= new ByteArrayOutputStream(); bmp.compress(Bitmap.
{“file_name”: “my_pic”, “content_base64”: “MKMD….”}
在上传之前,还需要压缩图像并对内容进行编码。我们目前的实施是:
Bitmap bmp = BitmapFactory.decodeFile(localPath);
ByteArrayOutputStream outputStream= new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
byte[] byteArrayImage = outputStream.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
JSONObject jObject = new JSONObject();
try {
jObject.put("file_name", "test.jpg");
jObject.put("content_base64", encodedImage);
String jObjectString = jObject.toString();
URL url = new URL(serverPath);
...
connection.setRequestMethod("PUT");
connection.setUseCaches(false);
OutputStreamWriter wr = new
OutputStreamWriter(connection.getOutputStream());
wr.write(jObjectString);
wr.close();
...
}
我想把上面的代码改成改装上传。在研究了使用OkHttp的RequestBody或MultipartBody.Part类之后。但是我不知道如何转换上面的代码
有什么建议吗 1.使用请求和添加方法创建接口
public interface PostRequests {
@PUT("your_url")
Call<YourResponse> upload(@Body YourRequest yourRequest);
}
3.初始化
public class Api {
private PostRequests mPostRequests;
public Api() {
mPostRequests = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("your_base_url")
.build()
.create(PostRequests.class);
}
public Call<YourResponse> upload(String localPath) {
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeFile(localPath);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
byte[] byteArrayImage = outputStream.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
YourRequest yourRequest = new YourRequest();
yourRequest.setFileName("test.jpg");
yourRequest.setPicture(encodedImage);
return mPostRequests.upload(yourRequest);
}finally {
if(bmp!=null)
bmp.recycle();
}
}
}
您可以从AT(Base64)上传字符串形式的图片,这样您就可以将该字符串放入pojo对象。您的请求是
JSON
所以不需要多部分。非常感谢。我得到了它。需要将其视为服务器的更新对象,而不是文件上载。我可以再问一个问题吗。如果文件太大,即使在压缩和使用图片内容编码字符串后,是否会出现问题?@啮齿动物_la这取决于服务器设置。
public class Api {
private PostRequests mPostRequests;
public Api() {
mPostRequests = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("your_base_url")
.build()
.create(PostRequests.class);
}
public Call<YourResponse> upload(String localPath) {
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeFile(localPath);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
byte[] byteArrayImage = outputStream.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
YourRequest yourRequest = new YourRequest();
yourRequest.setFileName("test.jpg");
yourRequest.setPicture(encodedImage);
return mPostRequests.upload(yourRequest);
}finally {
if(bmp!=null)
bmp.recycle();
}
}
}
public void uploadMethod()
{
Api api=new Api();
api.upload("path").execute().body();// it is sync operation you can use eneque() for async
}