Android 上传照片并通过RestApi发送
我获得了此端点,但我不知道如何从电话库拍照并通过此端点发送此图像?尝试此代码 将以下依赖项添加到应用程序级渐变文件中Android 上传照片并通过RestApi发送,android,Android,我获得了此端点,但我不知道如何从电话库拍照并通过此端点发送此图像?尝试此代码 将以下依赖项添加到应用程序级渐变文件中 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' 然后定义改装对
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);
}
公共接口{
@多部分
@张贴(“/signup/”)
调用registerUser(@PartMap);
}
api调用activivty
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());
}
});
}
private void uploadImage(){
ApiInterface ApiInterface=ApiClient.getInstance().getClient().create(ApiInterface.class);
文件=新文件(mAttachmentFilePath);
RequestBody mFile=RequestBody.create(MediaType.parse(“image/*”),文件);
MultipartBody.Part fileToUpload=MultipartBody.Part.createFormData(“profile_pic”,file.getName(),mFile);
RequestBody userObjectId=RequestBody.create(MediaType.parse(“text”),“hzucgm6gz”);
调用userProfileResponseCall=apiInterface.uploadImage(fileToUpload,userObjectId);
showProgress();
userProfileResponseCall.enqueue(新的回调(){
@凌驾
公共void onResponse(调用、响应){
if(response!=null&&response.body()!=null&&response.issusccessful()){
closeProgress();
Toast.makeText(getApplicationContext(),response.body().getMsg(),Toast.LENGTH\u LONG.show();
Log.d(“响应消息::”,Response.body().getMsg());
将(response.body().getAvatarUrl())加载到(mIvServerImage)中;
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
closeProgress();
Log.d(“错误::”,t.getMessage());
}
});
}
我希望你们知道如何打开画廊,并获得选定的图像路径。
若您在MarshallLow设备上运行应用程序,请添加权限模型。查看本教程,它可以帮助您使用改型将文件发送到服务器