我在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();
}
}
}
什麼是錯誤?顯示logcat – Nitesh
其BasicNetwork.performRequest:我的API的意外響應代碼500。 –
我認爲這是因爲您使用了錯誤的網址而發生的。當您提供錯誤的網址時,Volley會返回500。 – Nitesh