Android 使用 HttpPost/HttpGet 发送照片

标签 android post

我需要将存储在 SDCARD 中的文件中的照片发送到外部 Api。为此,我使用了以下代码:

String responseStr = null;
    this.setMethod(request);
    this.setParameters(tags, parameters, optional);
    String requested = mUri.build().toString();
    HttpHost host = new HttpHost(API_REST_HOST, -1,
            HttpHost.DEFAULT_SCHEME_NAME);
    Log.d(TAG, requested);      
    try { 
        HttpPost post = new HttpPost(requested);
        File file = new File(filepath); 
        FileEntity fileentity; 
        if (filepath.substring(filepath.length()-3, filepath.length 
                ()).equalsIgnoreCase("jpg")) { 

            fileentity = new FileEntity(file,"image/jpeg;"); 
            fileentity.setChunked(true); 
            post.setEntity(fileentity);
        } 
        post.addHeader("Content-Type", "image/jpeg;");
        HttpResponse response = mClient.execute(host,post);

“setMethod”和“setParameters”是构建 Uri 对象的自有方法。外部 api 接受参数但不接受照片。它期待 HttpBody 字段中的照片。

有什么想法吗?谢谢

谢谢

最佳答案

问题可能是内容类型中的分号:

fileentity = new FileEntity(file,"image/jpeg;");
                                            ^

关于Android 使用 HttpPost/HttpGet 发送照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3132104/

相关文章:

android - UNITY 触摸位置到世界位置 2D

android - 将具有空值的字符串作为参数传递时, Volley 字符串请求错误

python - 如何使用python请求正确形成对该网站的POST请求

java - 安卓 : how to return JSONObject from Volley OnRequest StringRequest method

android - 将设置按钮添加到应用程序信息屏幕

php - curl : * Violate RFC 2616/10. 3.2 并从 POST 切换到 GET

php - 对 PHP 表单感到沮丧 - jquery mobile

php - 使用 PHPUnit 发送 POST 请求

android - 如何使用 Retrofit 解决 IllegalArgumentException "Malformed content type: ......"

android - Facebook 状态更新