gpt4 book ai didi

android - 上传图片时com.android.volley.ServerError

转载 作者:太空狗 更新时间:2023-10-29 14:47:32 25 4
gpt4 key购买 nike

我在内部存储中有一张图像,我将其保存在 imageview 中。然后我将图像从 imageview 上传到服务器。但是我在上传时遇到了这个 Volley 错误。我不知道我的代码有什么问题。我完全按照本 tutorial 中的说明进行操作.我仍然收到错误。我想知道我的代码是否有任何错误。

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();
}

}




}

最佳答案

您的服务器可能有问题。检查 onErrorResponse 中的响应添加这个并检查 logcat:

 NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
Log.e("Volley", "Error. HTTP Status Code:"+networkResponse.statusCode);
}

if (error instanceof TimeoutError) {
Log.e("Volley", "TimeoutError");
}else if(error instanceof NoConnectionError){
Log.e("Volley", "NoConnectionError");
} else if (error instanceof AuthFailureError) {
Log.e("Volley", "AuthFailureError");
} else if (error instanceof ServerError) {
Log.e("Volley", "ServerError");
} else if (error instanceof NetworkError) {
Log.e("Volley", "NetworkError");
} else if (error instanceof ParseError) {
Log.e("Volley", "ParseError");
}

关于android - 上传图片时com.android.volley.ServerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38244446/

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