在我的应用程序中,我正在将图像上传到我公司的服务器
我遇到的问题是我希望服务器在图片上传后响应上传成功或类似的内容, 目前上传工作正常,但我想要来自服务器的成功/错误响应,我正在使用 koush Ion 进行上传,到目前为止我尝试过的任何方法都不起作用,并且尝试过谷歌搜索某些内容,但我找不到我要找的东西
上传图片代码
private void uploadImageToServer() {
try {
Bitmap bitmap=ImageLoader.init().from(selectedPhoto).requestSize(1024, 1024).getBitmap();
String encodedImage=ImageBase64.encode(bitmap);
Log.d(TAG, encodedImage);
CheckBox chk=findViewById(R.id.chk1);
if (chk.isChecked()) {
Uri.Builder builder=new Uri.Builder();
builder.scheme("https")
.authority("www.smartpractice.co.za")
.appendPath("files-upload-phone-app.asp")
.appendQueryParameter("MyForm", "Yes")
.appendQueryParameter("ClientID", clientId)
.appendQueryParameter("Username", email)
.appendQueryParameter("Pwd", pwd)
.appendQueryParameter("Category", Item)
.appendQueryParameter("ClientName", Item2)
.appendQueryParameter("NoEmail", "Yes");
myURL=builder.build().toString();
} else {
Uri.Builder builder4=new Uri.Builder();
builder4.scheme("https")
.authority("www.smartpractice.co.za")
.appendPath("files-upload-phone-app.asp")
.appendQueryParameter("MyForm", "Yes")
.appendQueryParameter("ClientID", clientId)
.appendQueryParameter("Username", email)
.appendQueryParameter("Pwd", pwd)
.appendQueryParameter("Category", Item)
.appendQueryParameter("ClientName", Item2)
.appendQueryParameter("NoEmail", "");
myURL=builder4.build().toString();
}
final ProgressDialog pd=new ProgressDialog(SecondActivity.this);
pd.setMessage("Uploading, Please Wait....");
pd.show();
File file=new File(selectedPhoto);
Ion.with(SecondActivity.this)
.load(myURL)
.uploadProgressDialog(pd)
.setMultipartFile("SP-LOG", "image/*", file)
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
Toast.makeText(SecondActivity.this, "Upload Successful", Toast.LENGTH_LONG).show();
pd.cancel();
}
});
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "No File Found", Toast.LENGTH_LONG).show();
}
}
最佳答案
假设您的服务通过调用 UploadImage API 成功上传图像后返回类似以下内容:
{
"message":"Upload successful",
"status":200
}
上述响应将由服务端 API 发送,而不是由 Android 发送。
//Your above business logic for selecting file
Ion.with(SecondActivity.this)
.load(myURL)
.uploadProgressDialog(pd)
.setMultipartFile("SP-LOG", "image/*", file)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
String message = result.getString("message");
Toast.makeText(SecondActivity.this,message, Toast.LENGTH_LONG).show();\
pd.cancel();
}
});
或者如果 API 只返回这样的字符串作为响应 “上传成功”
那么你可以这样写
//Your above business logic for selecting file
Ion.with(SecondActivity.this)
.load(myURL)
.uploadProgressDialog(pd)
.setMultipartFile("SP-LOG", "image/*", file)
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
Toast.makeText(SecondActivity.this,result, Toast.LENGTH_LONG).show();\
pd.cancel();
}
});
希望这对您有所帮助,如果没有请随时询问!
关于java - 如何从服务器获取响应,例如 "Upload Successful",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57687649/