gpt4 book ai didi

java - 使用 Android Volley 发送图像或文本或两者

转载 作者:行者123 更新时间:2023-11-30 01:24:32 24 4
gpt4 key购买 nike

我可以使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com