Android使用HttpPost / HttpGet发送照片

时间:2010-06-28 11:55:16

标签: 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 Field中的照片。

有什么想法吗?感谢

由于

1 个答案:

答案 0 :(得分:4)

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

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