2016-07-07 88 views
3

我在imageview中保存了内部存储器中的图像。然后我将图像从imageview上传到服务器。但是我在上传时遇到了这个Volley错误。我不知道我的代码中存在什么问题。我完全按照tutorial的指示完成。仍然有错误。我想知道我的代码中是否有任何错误。com.android.volley.ServerError while upload up image

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(); 
     } 

    } 




} 
+0

什么是错误?显示logcat – Nitesh

+0

其BasicNetwork.performRequest:我的API的意外响应代码500。 –

+0

我认为这是因为您使用了错误的网址而发生的。当您提供错误的网址时,Volley会返回500。 – Nitesh

回答

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"); 
    } 
+0

它的第一个错误,即'HTTP状态代码:500',我有互联网许可。 –

+0

那么,我的代码是否正确或者存在一些错误? –

+0

我在你的代码中找不到任何错误.. – Nitesh