Android 使用开源框架AsyncHttpClient上传文件到服务器

publicvoid postFile() throwsException{
    String path ="";
    File file = newFile(path);
    if(file.exists() && file.length()>0){
        AsyncHttpClient client = newAsyncHttpClient();
        RequestParams params = newRequestParams();
        params.put("profile_picture", file);
        client.post("http://192.168.1.100:8080/web/UploadFile", params,newAsyncHttpResponseHandler() {
             
            @Override
            publicvoid onSuccess(intstatusCode, Header[] headers, byte[] responseBody) {
                 
                Toast.makeText(MainActivity.this,"成功", Toast.LENGTH_LONG).show();
            }
             
            @Override
            publicvoid onFailure(intstatusCode, Header[] headers,
                    byte[] responseBody, Throwable error) {
                Toast.makeText(MainActivity.this,"失败", Toast.LENGTH_LONG).show();
            }
        });
    }else{
        Toast.makeText(this,"文件不存在",1).show();
    }
     
}


你可能感兴趣的:(Android 使用开源框架AsyncHttpClient上传文件到服务器)