我可以使用 Android Volley 库发送图像和文本。但是如何使用 Volley 库使图像可选发送呢?
当我尝试只发送文本时,抛出了一个 Java 空指针异常
。
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);
}
最佳答案
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;
}
};
关于java - 使用 Android Volley 发送图像或文本或两者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36590407/