2

I'm trying to post data to server with user name, user id and profile picture using android HttpPost. When I execute the http post, application crash and nothing shows in log. My code is as following :

protected String doInBackground(String... params) {
    if(params==null || params.length <= 0) return null;

    try {
        String apiUrl = "";


        File file = new File(params[2]);
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("name", new StringBody(params[0]));
        entity.addPart("userID", new StringBody(params[1]));
        entity.addPart("userfile", new FileBody(file));

        apiUrl = "/api/consumer/" +  Globals.USER_ID + "/profile/edit";
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(
                Globals.BASE_URL + apiUrl);

        httpPost.setEntity(entity);
        HttpResponse resp = httpClient.execute(httpPost); //crash here..
        String respStr = EntityUtils.toString(resp.getEntity());
        return respStr;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

Please help me to find out what was wrong in my code.

Thiha Zaw
  • 806
  • 1
  • 9
  • 25

0 Answers0