2011-07-22 20 views
5

Görüntüler genellikle özel HTTP üstbilgileri gerektirir:Çok bölüm için UrlEncodedFormEntity kullanarak Görüntüleri ve metni yükleyebilir miyim?

Content-disposition: attachment; filename="file2.jpeg" 
Content-type: image/jpeg 
Content-Transfer-Encoding: binary 

Kullanarak POST yapıyorum:

List<NameValuePair> formparams = new ArrayList<NameValuePair>(); 
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams); 

urlEncodedFormEntity setContentType'a izin veriyor, ancak görüntü ve metinleri MIX'e nasıl ekleyebilirim? ?

cevap

5
try { 
    File file = new File(Environment.getExternalStorageDirectory(),"FMS_photo.jpg"); 

    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost("http://homepage.com/path"); 
    FileBody bin = new FileBody(file); 


    Charset chars = Charset.forName("UTF-8"); 

    MultipartEntity reqEntity = new MultipartEntity(); 
    reqEntity.addPart("problem[photos_attributes][0][image]", bin); 
    reqEntity.addPart("myString", new StringBody("17", chars)); 

    post.setEntity(reqEntity); 
    HttpResponse response = client.execute(post); 

    HttpEntity resEntity = response.getEntity(); 
    if (resEntity != null) {  
    resEntity.consumeContent(); 
    } 

    return true; 

    } catch (Exception ex) { 

    globalStatus = UPLOAD_ERROR; 
    serverResponse = ""; 
    return false; 
    } finally { 

} 

sorun niteliği görüntüsünü taşıyacak ve myString dize taşımak ...

İlgili konular