如何在android中打印多部分实体的发送数据

时间:2016-08-29 08:35:36

标签: android multipartentity

我已经将多个图像和一些字符串数据上传到php服务器。我想打印想要我发送到服务器的数据。我试过这样的事情

类ImageUploadTask扩展了AsyncTask {

String sResponse = null;

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    dialog = ProgressDialog.show(Room_addroom1.this, "Uploading",
            "Please wait...", true);
    dialog.show();
}



@Override
protected String doInBackground(String... params) {
    try {

        String url ="http://airbnb.abservetech.com/demo/public/mobile/hotel/roomsadd";
        int i = Integer.parseInt(params[0]);
        System.out.println("i---"+i);


        SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
        String userID = prefs.getString("userid", null);

        Bitmap bitmap = decodeFile(map.get(i));

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        entity = new MultipartEntity();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] data = bos.toByteArray();


        byte[] res=bos.toByteArray();


        entity.addPart("room_images", new ByteArrayBody(data,
                "image/jpeg", params[1]));


        entity.addPart("user_id", new StringBody("52"));
        entity.addPart("room_type",new StringBody( "premium"));
        entity.addPart("room_prize", new StringBody("2356"));


        httpPost.setEntity(entity);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        entity.writeTo(bytes);
        Log.e("mainToPost", "roommainToPost----" + entity.toString());

        HttpResponse response = httpClient.execute(httpPost,
                localContext);
        sResponse = EntityUtils.getContentCharSet(response.getEntity());


    } catch (Exception e) {
        if (dialog.isShowing())
            dialog.dismiss();
        Log.e(e.getClass().getName(), e.getMessage(), e);

    }
    return sResponse;
}

@Override
protected void onPostExecute(String sResponse) {
    try {
        if (dialog.isShowing())
            dialog.dismiss();

        if (sResponse != null) {
            Toast.makeText(getApplicationContext(),
                    sResponse + " Photo uploaded successfully",
                    Toast.LENGTH_SHORT).show();
            count++;
            if (count < map.size()) {
                new ImageUploadTask().execute(count + "", "hm" + count
                        + ".jpg");
            }
        }

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), e.getMessage(),
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }

}

}

但输出显示如下

E/mainToPost: roommainToPost----org.apache.http.entity.mime.MultipartEntity@42f2d390

如何打印所有发送值?

1 个答案:

答案 0 :(得分:1)

 String entityContentAsString = new String(byteoutputstream.toByteArray());
    Log.e("multipartEntitty:", "" + entityContentAsString);

试试这个。