上传图片时在multipartResquest中获取org.apache.http.conn.BasicManagedEntity@52dd3be8的响应

时间:2015-08-11 07:43:03

标签: java android

我在服务的标题中上传Base 64,我将状态行设置为200 OK但我收到的响应是org.apache.http.conn.BasicManagedEntity@52dd3be8 我应该得到响应" ../ Img_Category / 1_1_671.png"。

这是我的代码。

尝试{

        String str = twoHyphens + boundary + lineEnd;
        String str2 = "Content-Disposition: form-data; name=\"jsonFile\"";
        String str3 = "Content-Type: application/json";
        String str4 = "Content-Disposition: form-data; name=\"imgName\"";
        String str5 = "Content-Type: image/jpeg";
        String str6 = twoHyphens + boundary + twoHyphens;

        String encodedImage = Base64.encodeToString(imageByteArray,
                Base64.DEFAULT);

        String StrTotal = str + str2 + "\r\n" + str3 + "\r\n" + "\r\n"
                + str + str4 + "\r\n" + str5 + "\r\n" + "\r\n"
                + encodedImage + "\r\n" + str;

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        String boundary = "-------------" + System.currentTimeMillis();
        httpPost.addHeader("Content-type", "multipart/form-data; boundary="+ boundary);
        httpPost.addHeader("ImgSrc", encodedImage);

        StringEntity se = new StringEntity(StrTotal);
        se.setContentEncoding("UTF-8");
        httpPost.setEntity(se);

        response = httpClient.execute(httpPost);
        Log.i("response", response + "");

        int responseString = response.getStatusLine().getStatusCode();
        Log.i("responseString", responseString + "");

        HttpEntity entity = response.getEntity();
        getResponseText = entity.toString();
        Log.i("getResponseText", getResponseText);

    } catch (Exception e) {
        e.printStackTrace();
        Log.i("uploadImageException", e.toString());
    }

请帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

你应该得到这样的回复内容:

private static String getStringContent(HttpResponse response) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
        String body;
        String content = "";

        while ((body = bufferedReader.readLine()) != null) {
            content += body + "\n";
        }
        return content.trim();
    }