2010-06-28 49 views
1

我需要將存儲在SDCARD中的文件的照片發送到外部Api。爲了做到這一點,我使用下面的代碼:Android使用HttpPost/HttpGet發送照片

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字段中的照片。

有什麼想法?由於

感謝

+0

所以你要發送的JPEG的身體,但服務器是說,它沒有看到JPEG在身體裏? – 2010-06-28 16:05:13

+0

服務器以這種方式使用php拍攝圖像 if($ im_s = file_get_contents('php:// input')){ – xger86x 2010-06-28 16:38:20

回答

4

的問題可能是在內容類型分號:

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