1

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

java.net.SocketException:sendto 失败:ECONNRESET(对等方重置连接)

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

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

2 回答 2

2

可以肯定的是,您需要将您的更改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班级的定义如此处接受的答案所示,如下所示:

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 上传图片进行了一些小的修改。我希望这对您有所帮助,并且其他人也可能会发现它有用。

于 2016-05-11T10:27:58.043 回答
1

当我不使用 url 编码查询时,我通常会收到此错误。

看看Android 中的 URL 编码

于 2019-07-15T13:58:49.497 回答