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);
}
如果我正確理解你的問題,那麼你需要刪除'params.put(KEY_IMAGE,銳美);'從你的代碼中可選作,或者你可以檢查它的空值'if(someImage!= null){params.put(KEY_IMAGE,rimage); }' –