Android Volley ECONNRESET [英] Android Volley ECONNRESET

查看:162
本文介绍了Android Volley ECONNRESET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Volley 库并将图像上传到服务器。此库应以独立模式执行此过程,但是会收到以下错误消息:

I try to use Volley library and upload image to server. This library should do this process in standalone mode, but have got following error message:


java.net.SocketException:sendto failed:ECONNRESET连接重置
由对等)

java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)

这可能是服务器端配置错误?
我尝试使用以下代码上传 jpeg 图像:

Is it maybe a server side misconfiguration? I try to upload a jpeg image with this code:

private void uploadImage(){
    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) {
                    loading.dismiss();
                    Toast.makeText(PhotoActivity.this, s , Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    loading.dismiss();

                    Toast.makeText(PhotoActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            String image = getStringImage(bitmap);
            String name = editTextName.getText().toString().trim();

            Map<String,String> params = new Hashtable<String, String>();

            params.put(KEY_IMAGE, image);
            params.put(KEY_NAME, name);

            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}


推荐答案

需要将您的 uploadImage()更改为:

private void uploadImage(){
    final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
    //here you use your custom multi-part-request as I suggested in the comment:
    ImageUploadRequest imageUploadReq = new ImageUploadRequest(UPLOAD_URL,            
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    loading.dismiss();

                    Toast.makeText(PhotoActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            },
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    loading.dismiss();
                    Toast.makeText(PhotoActivity.this, s , Toast.LENGTH_LONG).show();
                }
            }, yourImageFile);

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(imageUploadReq);
}

您的 ImageUploadRequest 课程的定义如下:此处中所示的答案:

Where your ImageUploadRequest class is defined as demonstrated in the accepted answer here like this:

public class ImageUploadRequest<T> extends Request<T> {

private static final String FILE_PART_NAME = "file";

private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create();
private final Response.Listener<T> mListener;
private final File mImageToUpload;
protected Map<String, String> headers;

public ImageUploadRequest(String uploadURL, ErrorListener errorListener, Listener<T> listener, File imageFileToUpload){
    super(Method.POST, uploadURL, errorListener);

    mListener = listener;
    mImageToUpload = imageFileToUpload;
    //call the helper method to build the multipart entity
    buildMultipartEntity();
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = super.getHeaders();

    if (headers == null || headers.equals(Collections.emptyMap())) {
        headers = new HashMap<String, String>();
    }

    headers.put("Accept", "application/json");

    return headers;
}

private void buildMultipartEntity(){
    mBuilder.addBinaryBody(FILE_PART_NAME, mImageToUpload, ContentType.create("image/jpeg"), mImageToUpload.getName());
    mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));
}

@Override
public String getBodyContentType(){
    String contentTypeHeader = mBuilder.build().getContentType().getValue();
    return contentTypeHeader;
}

@Override
public byte[] getBody() throws AuthFailureError{
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        mBuilder.build().writeTo(bos);
    } catch (IOException e) {
        VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request.");
    }

    return bos.toByteArray();
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    T result = null;
    return Response.success(result, HttpHeaderParser.parseCacheHeaders(response));
}

@Override
protected void deliverResponse(T response) {
    mListener.onResponse(response);
}
}

我已经对使用Google Volley上传图片到您的具体情况。我希望这可以帮助你,而其他人也可能会发现它很有用。

I have made some minor adaptations of Upload an image using Google Volley to your specific situation. I hope this helps you and that others may also find it useful.

这篇关于Android Volley ECONNRESET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆