2014-12-05 18 views
7

Próbuję wysłać obraz do mojego API. ale podczas korzystania z MultipartEntity StringBody pojawia się błąd, ponieważ StringBody (String) jest przestarzałe.Android StringBody (String) jest przestarzałe

Nie działa. Tak więc próbka pod adresem Android sending image to Server via MultipartEntity - setting Content-type?.

jest to kod:

try { 

    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("api_key", api_key)); 
    params.add(new BasicNameValuePair("access_token", access_token)); 

    API api = new API(mApiKey, mApiSecret);   


    HttpClient client = new DefaultHttpClient(); 
    HttpPost postMethod = new HttpPost("MY API URL"); 
    File file = new File(imagePath); 
    MultipartEntity entity = new MultipartEntity(); 
    FileBody contentFile = new FileBody(file); 

    StringBody api_key  = new StringBody(mApiKey); 
    StringBody access_token = new StringBody(access_token); 

    entity.addPart("api_key", api_key); 
    entity.addPart("hash", access_token); 
    entity.addPart("image", contentFile); 

    postMethod.setEntity(entity); 
    client.execute(postMethod); 



} catch (Exception e) { 
    e.printStackTrace(); 
} 

Odpowiedz

16

Konstruktor, którego używasz do tworzenia nowej instancji StringBody jest przestarzała.

Zamiast

new StringBody(mApiKey); 

należy użyć StringBody konstruktora z drugim parametrem, który jest ContentType jak

new StringBody(mApiKey, ContentType.TEXT_PLAIN); 

W celu uzyskania dalszych informacji znajdziesz pod adresem:

http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/StringBody.html

i

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/ContentType.html

+0

Również typ MultipartEntity jest przestarzała. – Joolah

+0

Tak, dokumentację można łatwo znaleźć w Google: http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html – Thorben

Powiązane problemy