使用带MultipartEntity的POST(HttpURLConnection)发送文件和数据

时间:2014-12-12 07:45:30

标签: android android-image android-bitmap androidhttpclient

我使用以下代码发送数据。

问题是在服务器端只收到底部的2个值,并且最高值未定义,即

reqEntity.addPart("eid", "1"); -->this value gives undefined index
reqEntity.addPart("cid", "2");
reqEntity.addPart("caid", "2");

底部的2个值到达服务器..

我的代码是

 protected Void doInBackground(Bitmap... bitmaps) {
        if (bitmaps[0] == null)
            return null;
        setProgress(0);
        Bitmap bitmap = bitmaps[0];
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); // convert Bitmap to ByteArrayOutputStream
        InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert ByteArrayOutputStream to ByteArrayInputStream

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://xyz/save.php"); // server
            try {
                SessionManager session = new SessionManager(getApplicationContext());
                // get user data from session
                HashMap<String, String> user = session.getUserDetails();
                // id
                String company_id3 = user.get(SessionManager.KEY_CID);
                String emp_id3 = user.get(SessionManager.KEY_EID);




                MultipartEntity  reqEntity = new MultipartEntity();
                reqEntity.addPart("myFile",
                        System.currentTimeMillis() + ".jpg", in);
               reqEntity.addPart("eid", "1");
               reqEntity.addPart("cid", "2");
                reqEntity.addPart("caid", "2");

                httppost.setEntity(reqEntity);
                HttpResponse response = httpclient.execute(httppost);
                response.getStatusLine().getStatusCode();
                HttpEntity getResponseEntity = response.getEntity();
                responseString = EntityUtils.toString(getResponseEntity);

                Log.d("Response", responseString);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }


        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (stream != null) {
            try {
                stream.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return null;

    }


}

我已经尝试将其他两个值后的eid移到底部..顶部值无法到达,底部2到达服务器。

0 个答案:

没有答案