7

I am trying to send a image to my API. but while using MultipartEntity StringBody i get error as StringBody(String) is deprecated.

I doesn't work. I so a sample at Android sending image to Server via MultipartEntity - setting Content-type?.

this is the code:

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();
}
Community
  • 1
  • 1
Thiago
  • 12,778
  • 14
  • 93
  • 110

2 Answers2

16

The constructor that you use to create a new instance of StringBody is deprecated.

Instead of

new StringBody(mApiKey);

you should use a StringBody constructor with a second parameter that is a ContentType like

new StringBody(mApiKey, ContentType.TEXT_PLAIN);

For further information look at:

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

and

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

Thorben
  • 1,019
  • 7
  • 20
  • Also the The type MultipartEntity is deprecated. – Thiago Dec 05 '14 at 08:03
  • Yes it is, the documentation can easily be found with Google: http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html – Thorben Dec 05 '14 at 09:01
2

StringBody :

 entity.addPart("api_key", new StringBody(mApiKey,ContentType.TEXT_PLAIN));

MultipartEntity :

Check : The type MultipartEntity is deprecated

Community
  • 1
  • 1
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67