我想将我的应用中的图片上传到服务器。因为无法进行ftp连接,所以我想通过POST上传图片作为请求。
我在我的应用中使用排球。当我将pic转换为Base64字符串并将其发送到我的服务器时,我收到警告:
D / Volley:[50697] BasicNetwork.logSlowRequests:HTTP响应 request =< [] http://domain.com/api.php 0x3ce314bb NORMAL 1> [lifetime = 5264],[size = 2],[rc = 200],[retryCount = 0]
这是我的上传课程:
public class Upload {
private Context context;
public Upload(Context _context){
context = _context;
}
public void upload(final File file){
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST,"http://domain.com/api.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("DEBUG","RESPONSE: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<>();
params.put("a","text");
params.put("b",convertPic(file.getPath()));
params.put("c","text");
return params;
}
};
queue.add(sr);
}
private String convertPic(String path)
{
Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
private String convertString(String s){
return Base64.encodeToString((s).getBytes(),Base64.NO_WRAP);
}
}
为了测试,我的php脚本只检查$ _POST字段:
<?php
header('content-type: text/plain; charset=utf-8');
header("access-control-allow-origin: *");
error_reporting(E_ALL);
ini_set('display_errors', 1);
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if(isset($post['a']) && $post['a'] != "" && isset($post['b']) && $post['b'] != "" && isset($post['c']) && $post['c'] != ""){
echo 'OK';
}
答案 0 :(得分:0)
添加重试:
sr.setRetryPolicy(new DefaultRetryPolicy(0,0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Make:DefaultRetryPolicy.DEFAULT_MAX_RETRIES为0