Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过android应用程序上传图像并发送带有$\u POST的文本_Php_Android_Post_Httprequest_Image Uploading - Fatal编程技术网

Php 通过android应用程序上传图像并发送带有$\u POST的文本

Php 通过android应用程序上传图像并发送带有$\u POST的文本,php,android,post,httprequest,image-uploading,Php,Android,Post,Httprequest,Image Uploading,我必须上传一个图像到我的服务器,但也发送一些文本。这是我所做的,但不起作用: String UploadImage(Bitmap bm) { String resp = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bm.compress(CompressFormat.JPEG, 75, bos); byte[] data = bos.

我必须上传一个图像到我的服务器,但也发送一些文本。这是我所做的,但不起作用:

String  UploadImage(Bitmap bm) {
    String resp = null;
    try {  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bm.compress(CompressFormat.JPEG, 75, bos);
        byte[] data = bos.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("myserver.com/add.php");
        String rndName =generateSessionKey(15);

        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("filename",rndName));
        nameValuePair.add(new BasicNameValuePair("email",email));

        UrlEncodedFormEntity form;
        form = new UrlEncodedFormEntity(nameValuePair);
        form.setContentEncoding(HTTP.UTF_8);
        try {
            postRequest.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }

        ByteArrayBody bab = new ByteArrayBody(data,rndName+".jpg");

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("uploaded", bab);
        //reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();
        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        resp=s.toString();
    } catch (Exception e) {
        // handle exception here
        Log.e(e.getClass().getName(), e.getMessage());
    }
    return resp;
}
字符串上传图像(位图bm){
字符串resp=null;
试试{
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
压缩(CompressFormat.JPEG,75,bos);
字节[]数据=bos.toByteArray();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost postRequest=newhttppost(“myserver.com/add.php”);
字符串rndName=generateSessionKey(15);
列表名称ValuePair=新的ArrayList(1);
添加(新的BasicNameValuePair(“文件名”,rndName));
添加(新的BasicNameValuePair(“email”,email));
UrlEncodedFormEntity表单;
表单=新的UrlEncodedFormEntity(nameValuePair);
setContentEncoding(HTTP.UTF_8);
试一试{
setEntity((HttpEntity)新的UrlEncodedFormEntity(nameValuePair,“UTF-8”);
}捕获(不支持的编码异常e){
//将错误写入日志
e、 printStackTrace();
}
ByteArrayBody bab=新的ByteArrayBody(数据,rndName+“.jpg”);
MultipartEntity reqEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
请求实体添加部分(“上传”,bab);
//要求实体添加部分(“光适应”,新纵梁主体(“sfsdfsdf”);
postRequest.setEntity(reqEntity);
HttpResponse response=httpClient.execute(postRequest);
BufferedReader=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent(),“UTF-8”);
字符串响应;
StringBuilder s=新的StringBuilder();
而((sResponse=reader.readLine())!=null){
s=s.append(sResponse);
}
resp=s.toString();
}捕获(例外e){
//在这里处理异常
Log.e(e.getClass().getName(),e.getMessage());
}
返回响应;
}
服务器无法获取
文件名
电子邮件

为什么不起作用


谢谢你的帮助

不能将URL编码和多部分合并。相反,将所有字符串字段添加为多部分的一部分

请看下面的示例:

不能将URL编码和多部分合并。相反,将所有字符串字段添加为多部分的一部分

请看下面的示例: