如何解决Apollo graphql android在上传文件时给出http 400错误请求错误
当我在windows中使用curl上传文件到graphql服务器时,结果是成功的:
curl -X POST -F query="mutation { uploadAvatar(avatar: \"avatar\")}" -F avatar=@myImage.png http://94.182.215.114:4000/api
{"data":{"uploadAvatar":"success"}}
但是当我在 Android 中上传基于 this document 的文件时,它给出了 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)
上传头像.graphql:
mutation UploadAvatar($file:Upload!) {
uploadAvatar(avatar: $file)
}
java代码:
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 适用于其他查询。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。