【问题标题】:How can I get a response from the server such as "Upload Successful"如何从服务器获得响应,例如“上传成功”
【发布时间】:2020-01-01 09:36:58
【问题描述】:

在我的应用程序中,我将图像上传到我的公司服务器

我正在努力解决的问题是我希望服务器在上传图片后响应上传成功或类似的内容, 目前上传有效,但我希望服务器响应成功/错误,我正在使用 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();
        }

    }

【问题讨论】:

  • 为了做到这一点,您的服务器 api 必须返回您想要显示的消息。然后您可以在 onCompleted 回调中访问结果,并可以执行类似这样的操作 result == "SUCCESS" 。
  • 我已经编码了 3 周,你是怎么做到的?,非常感谢一个链接
  • 那是你的服务器软件需要改变,你不能强迫服务器从客户端做你想做的事。

标签: java php android json


【解决方案1】:

假设您的服务在通过调用 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();


                        }
                    });

或者 incase 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();


                        }
                    });

希望对你有帮助,如果没有,请随时提问!

【讨论】:

  • 好的,我会测试并回复你,谢谢老兄
  • 我得到无法解决 getString 上的错误,有什么建议吗?
  • 试试 result.get("message").getAsString();
  • 好的,但是我在吐司上得到了 NULL 指针,我的响应在一个 json 数组中,看起来像这样 MyJasonString = "" MyJasonString = MyJasonString &amp; "{""failed"": 1,""Errors"": [" MyJasonString = MyJasonString &amp; "{""reason"": ""文件过大""}" MyJasonString = MyJasonString &amp; "]}" response.write MyJasonString
猜你喜欢
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
相关资源
最近更新 更多