Android应用程序在使用MultipartEntity执行http post时崩溃

时间:2015-11-25 13:44:25

标签: android multipartentity

我正在尝试使用android HttpPost将数据发布到具有用户名,用户ID和个人资料图片的服务器。当我执行http帖子时,应用程序崩溃并且日志中没有显示任何内容。我的代码如下:

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;
}

请帮我查一下我代码中的错误。

0 个答案:

没有答案