上传图片时出现com.android.volley.ServerError

时间:2016-07-07 11:36:18

标签: android android-volley

我在图像视图中保存了内部存储中的图像。然后我将图像从imageview上传到服务器。但是我在上传时遇到了这个Volley Error。我不知道我的代码中有什么问题。我完全按照tutorial的指示行事。我仍然得到错误。我想知道我的代码中是否有任何错误。

public class NewActivity extends AppCompatActivity{

    Button save, upload;
    ImageView image;
    String encodedImage;
    private static final String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/UbookSpa";
    private static final String IMGUPLOAD = "http://example.com/android/showcustimage";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        save = (Button)findViewById(R.id.save);
        upload = (Button)findViewById(R.id.up);
        image = (ImageView)findViewById(R.id.image);



        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadImage();

            }
        });

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageupload();
            }
        });
    }





    private void imageupload() {

        final String imagenew = getStringImage();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, IMGUPLOAD,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        try {
                            JSONObject jObj = new JSONObject(response);
                            if(jObj.getString("status").equals("1")){
                                Toast.makeText(NewActivity.this, "Its all good", Toast.LENGTH_SHORT).show();
                            }



                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "VolleyError:" + "\t" + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {

               // String image = getStringImage();

                Map<String, String> params = new HashMap<String, String>();
                params.put("userimage", imagenew);
                return params;
            }


        };

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }

    public String getStringImage(){

        image.buildDrawingCache();
        Bitmap bmap = image.getDrawingCache();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

        return encodedImage;
    }



    private void loadImage(){

        File directory = new File(file_path);
        File file = new File(directory, "myimage.jpg");
        try {
            FileInputStream fis = new FileInputStream(file);
            Bitmap bmap = BitmapFactory.decodeStream(fis);
            image.setImageBitmap(bmap);
            fis.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }




}

1 个答案:

答案 0 :(得分:0)

您的服务器可能存在问题。检查onErrorResponse中的响应添加它并检查logcat:

 NetworkResponse networkResponse = error.networkResponse;
    if (networkResponse != null) {
        Log.e("Volley", "Error. HTTP Status Code:"+networkResponse.statusCode);
    }

    if (error instanceof TimeoutError) {
        Log.e("Volley", "TimeoutError");
    }else if(error instanceof NoConnectionError){
        Log.e("Volley", "NoConnectionError");
    } else if (error instanceof AuthFailureError) {
        Log.e("Volley", "AuthFailureError");
    } else if (error instanceof ServerError) {
        Log.e("Volley", "ServerError");
    } else if (error instanceof NetworkError) {
        Log.e("Volley", "NetworkError");
    } else if (error instanceof ParseError) {
        Log.e("Volley", "ParseError");
    }