android - 上传照片并通过 RestApi 发送
解决方案
试试这个代码..
将以下依赖添加到应用程序级别的 gradle 文件中。
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
然后定义改造对象..
public class ApiClient {
private final static String BASE_URL = "https://api.github.com";
public static ApiClient apiClient;
private Retrofit retrofit = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
制作api接口..
public interface ApiInterface {
@Multipart
@POST("/signup/")
Call<UserResponseVo> registerUser(@PartMap Map<String, RequestBody> map);
}
和 api 调用活动 ..
private void uploadImage() {
ApiInterface apiInterface= ApiClient.getInstance().getClient().create(ApiInterface.class);
file=new File(mAttachmentFilePath);
RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("profile_pic", file.getName(), mFile);
RequestBody userObjectId = RequestBody.create(MediaType.parse("text"), "HZucg1m6Gz");
Call<UserProfileResponse> userProfileResponseCall=apiInterface.uploadImage(fileToUpload,userObjectId);
showProgress();
userProfileResponseCall.enqueue(new Callback<UserProfileResponse>() {
@Override
public void onResponse(Call<UserProfileResponse> call, Response<UserProfileResponse> response) {
if (response!=null && response.body()!=null && response.isSuccessful()){
closeProgress();
Toast.makeText(getApplicationContext(),response.body().getMsg(),Toast.LENGTH_LONG).show();
Log.d("Response Message::",response.body().getMsg());
Glide.with(MainActivity.this).load(response.body().getAvatarUrl()).into(mIvServerImage);
}
}
@Override
public void onFailure(Call<UserProfileResponse> call, Throwable t) {
closeProgress();
Log.d("Error::",t.getMessage());
}
});
}
我希望你知道如何打开画廊并获得选定的图像路径。如果您在 marshallow 设备上运行应用程序,则添加权限模型。
推荐阅读
- r - 如何基于整个矩阵离散化数据帧,每行包含 NA
- qt - 为什么在 QTQuick 2/QML 页面定义中无法识别 ScrollablePage?
- mongodb - 即使权限正确,mongodb.lock 权限被拒绝也无法启动 systemctl
- java - 如何重现代码规范中提到的多线程错误?(TSM03-J。不要发布部分初始化的对象)
- google-cloud-platform - GCP 作曲家环境 - 无法创建环境
- postgresql - 以微秒为单位的 unix 时间戳到当前时区的时间戳
- optimization - 使用 GEKKO 对大量变量、边界和约束进行优化
- java - firestore 快照监听器的生命周期和定价之间有什么关系?
- spring - 连接到 Okta 时在 Webclient 上获取 readAddress() 异常
- javascript - 即使使用了 escapeCharacter 函数,RegExp 值仍然不能与 .match 一起使用