2016-04-13 99 views
1

我可以使用Android Volley库发送图像和文本。但是怎样才能使用Volley库发送图像进行发送?使用Android Volley发送图像或文本或两者

当我试图只发送文本Java null pointer exception被抛出。

private void uploadImage(){ 
    //Showing the progress dialog 
    final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false); 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String s) { 
      //Disimissing the progress dialog 
      loading.dismiss(); 
      //Showing toast message of the response 
      Toast.makeText(PostActivity.this, s, Toast.LENGTH_LONG).show(); 
     } 
    }, 
    new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      //Dismissing the progress dialog 
      loading.dismiss(); 

      //Showing toast 
      Toast.makeText(PostActivity.this, volleyError.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }) { 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      //Converting Bitmap to String 
      String rimage = null; 
      try { 
       rimage = getStringImage(bitmap); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 

      //Getting Image Name 
      String FName = FoodName.getText().toString().trim(); 
      String LName = user.username; 

      //Creating parameters 
      Map<String, String> params = new Hashtable<String, String>(); 

      //Adding parameters 
      params.put(KEY_IMAGE, rimage); 
      params.put(KEY_NAME, FName); 
      params.put(KEY_LOGIN,LName); 

      //returning parameters 
      return params; 
     } 
    }; 

    //Creating a Request Queue 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 

    //Adding request to the queue 
    requestQueue.add(stringRequest); 
} 
+1

如果我正确理解你的问题,那么你需要删除'params.put(KEY_IMAGE,锐美);'从你的代码中可选作,或者你可以检查它的空值'if(someImage!= null){params.put(KEY_IMAGE,rimage); }' –

回答

0
@Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      //Converting Bitmap to String 
      String image = getStringImage(bitmap); 

      //Getting Image Name 
      String name = editTextName.getText().toString().trim(); 

      //Creating parameters 
      Map<String,String> params = new Hashtable<String, String>(); 

      //Adding parameters 
      params.put(KEY_IMAGE, image); 
      params.put(KEY_NAME, name); 

      //returning parameters 
      return params; 
     } 
1
You can check you image string as null or not ! If it is not null then send it. 


    @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       //Converting Bitmap to String 
       String rimage = null; 
       try { 
        rimage = getStringImage(bitmap); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 

       //Getting Image Name 
       String FName = FoodName.getText().toString().trim(); 
       String LName = user.username; 

       //Creating parameters 
       Map<String, String> params = new Hashtable<String, String>(); 

       //Adding parameters 
       if(rimage!=null) 
       { 
       params.put(KEY_IMAGE, rimage); 
       } 
       params.put(KEY_NAME, FName); 
       params.put(KEY_LOGIN,LName); 

       //returning parameters 
       return params; 
      } 
     }; 
相关问题