无法将图像和一个数据上传到服务器

时间:2017-08-29 04:48:17

标签: android json web-services

这是我用来上传一张照片和一张数据的代码。应用程序不断显示发送对话框,它不会停止。

private void uploadImage() {
        String uploadUrl = "http://************.php";

        HashMap<String, String> params = new HashMap<>();
        params.put("photo_one",imageString(bitmap));
        params.put("mobile",mName.getText().toString().trim());

        final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Sending Data....");
        progressDialog.show();

        JsonObjectRequest req = new JsonObjectRequest(uploadUrl, new JSONObject(params),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        progressDialog.dismiss();

                        String success = (String) response.opt("success");
                        if (!success.equals("1") )
                         {
                Toast.makeText(getApplicationContext(),"Invalid Username & Password...",Toast.LENGTH_LONG).show();
                         }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
            }
        });
        int x=2;// retry count
        req.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48, x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

我使用调试模式检查发生了什么,得到了这个回复。

> req = {JsonObjectRequest@4643} "[ ]
> http://*********.php 0xb5b5014d NORMAL null" 
> this = {MainActivity@4174}  uploadUrl = "http://***********.php"
> params = {HashMap@4549}  size = 2 
> progressDialog = {ProgressDialog@4595}  req = {JsonObjectRequest@4643} "[]
> http://***********.php 0xb5b5014d NORMAL null" x = 2

1 个答案:

答案 0 :(得分:0)

请使用Log查看Json Response。

要连续显示“进度”对话框,请更改以下内容

final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Sending Data....");
        progressDialog.show();

final ProgressDialog loading = ProgressDialog.show(this,"Sending Data","Please wait...",false,false);