Android VolleyPlus - SimpleMultiPartRequest问题。我无法发送文件并返回post params

时间:2015-10-22 22:16:00

标签: android android-volley

我正在使用VolleyPlus,我无法发送文件的params(addFile)或post(addMultipartParam):

        Response.Listener successListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("teste","SUCCESS: " + response);
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.i("teste", "ERROR: " + volleyError);
        }
    };

    SimpleMultiPartRequest request = new SimpleMultiPartRequest(
            Request.Method.POST, url, successListener,
            errorListener) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            // headers.put("Accept", "multipart/form-data");
            // headers.put("Content-Type", "application/json");
             headers.put("Content-Type", "text/plain");
            //headers.put("Content-Length", "302");
            headers.put("Content-Type","application/x-www-form-urlencoded");

            Log.i("teste","HEADER: " + headers);

            return headers;
        }
    };

    request.addMultipartParam("name", "text/plain", "my name");
    request.addFile("file1",img1);

    requestQueue = RequestQueueSingle.getInstance(RelatarErros.this);
    request.setShouldCache(false);
    request.setRetryPolicy(new DefaultRetryPolicy((int) TimeUnit.SECONDS.toMillis(40), DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue.add(request);
    requestQueue.start();

在回复时,我的帖子总是有空白,而$ _File ['file1']是空的。

PHP代码:

<?php 

header("Content-Type: text/plain");


echo "NAME: " . $_POST['name'];

if(isset($_FILES['file1'])){

    if (move_uploaded_file($_FILES['file']['tmp_name'], $directory . $final_name)) {

       echo "Success!";

    } else {

       echo "Error";
    }   
}

else{
    echo "Empty";
}


?>

一切都正确吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我这样做有点不同。请检查此链接:http://team.158ltd.com/2015/04/30/how-to-upload-image-to-php-server-android/

它对我有用。

我希望能帮助别人。