Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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
如何在android中使用httppost在服务器上发送图像数据?_Android_Http Post - Fatal编程技术网

如何在android中使用httppost在服务器上发送图像数据?

如何在android中使用httppost在服务器上发送图像数据?,android,http-post,Android,Http Post,我尝试将图像数据转换为“String str=Base64.encodeToString(imagedata,Base64.DEFAULT);”,然后我尝试发送它,但它不起作用 就连我也在下面试过 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(MIME.UTF8_CHARSET); builder.addBinaryBody

我尝试将图像数据转换为“String str=Base64.encodeToString(imagedata,Base64.DEFAULT);”,然后我尝试发送它,但它不起作用

就连我也在下面试过

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(MIME.UTF8_CHARSET);

        builder.addBinaryBody("file",imagedata);

        httpPost.setEntity(builder.build()); 
我仍然无法发送图像

我试图发送的服务器url如下所示

“”

请给我一些建议


谢谢。

我认为您的url不应该包含参数。您使用的任何参数都必须作为文本添加到生成器中。下面是我最近用来做同样事情的一些代码:

@Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String textFileName = params[0];
        String message = "This is a multipart post";
        String result =" ";
        HttpPost post = new HttpPost("http://10.0.2.2/test/upload_file_test.php");
        File file = new File(textFileName);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addBinaryBody("uploaded_file", file, ContentType.DEFAULT_BINARY, textFileName);
        builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);

        HttpEntity entity = builder.build();
        post.setEntity(entity);
        HttpClient client = new DefaultHttpClient();
        try {
            HttpResponse response = client.execute(post);
            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);
            }
            System.out.println("Response: " + s);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return message;
    }

}

我使用文本部分发送带有图像的消息。

我尝试使用您提供的代码。我将“”作为url。然后尝试实现MultipartEntityBuilder=MultipartEntityBuilder.create();builder.setMode(HttpMultipartMode.BROWSER兼容);builder.addBinaryBody(“file”,imagedata,ContentType.DEFAULT\u BINARY,“file”);\n但它给了我“Nosuchfield”“错误。请复制您的错误日志并将其挂起,以便我们可以查看问题的根源。还提供任何上下文。