#android #file-upload #apollo #apollo-client #graphql-java
Вопрос:
Результат будет успешным, когда я загружу файл на сервер graphql с помощью curl в Windows:
curl -X POST -F query="mutation { uploadAvatar(avatar: "avatar")}" -F avatar=@myImage.png http://94.182.215.114:4000/api
{"data":{"uploadAvatar":"success"}}
Но когда я загружаю файл на основе этого документа в Android, он выдает HTTP 400 Bad Request
ошибку:
логкат :
2021-07-31 20:27:30.057 11328-12262/? W/System.err: com.apollographql.apollo.exception.ApolloHttpException: HTTP 400 Bad Request
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor.parse(ApolloParseInterceptor.java:108)
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor$1.onResponse(ApolloParseInterceptor.java:53)
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor$executeHttpCall$1.onResponse(ApolloServerInterceptor.kt:114)
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-07-31 20:27:30.057 11328-12262/? W/System.err: at java.lang.Thread.run(Thread.java:929)
uploadAvatar.graphql :
mutation UploadAvatar($file:Upload!) {
uploadAvatar(avatar: $file)
}
javaCode :
ApolloClient apolloClient = ApolloClient.builder().serverUrl("http://94.182.215.114:4000/api")
.okHttpClient(new OkHttpClient.Builder().addInterceptor(new AuthorizationInterceptor(this)).build())
.build();
UploadAvatarMutation mutation = UploadAvatarMutation.builder()
.file(new FileUpload("image/jpeg", new FileUtils(this).getPath(resultAvatarUri)))
.build();
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.show();
apolloClient.mutate(mutation).enqueue(new ApolloCall.Callback<UploadAvatarMutation.Data>() {
@Override
public void onResponse(@NotNull Response<UploadAvatarMutation.Data> response) {
runOnUiThread(() -> {
progressDialog.dismiss();
new AlertDialog.Builder(LoginActivity.this, R.style.RoundedCornersDialog)
.setMessage(response.toString())
.setNegativeButton(android.R.string.ok, null)
.show();
});
}
@Override
public void onFailure(@NotNull ApolloException e) {
e.printStackTrace();
runOnUiThread(() -> {
progressDialog.dismiss();
new AlertDialog.Builder(LoginActivity.this)
.setMessage("Failure " e.getMessage())
.show();
});
}
});
Примечание: Apollo хорошо работает для других запросов.