how to send data to server through MultipartEntity

1.1k views Asked by At

am developing an application that can send an image and a user's first name and second name to the server. The application can send the image to the server but I have failed to retrieve user's first name and second name. Below is my code

java

HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(
                    "http://192.168.1.144/app_api/samplerEG.php");

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            entity.addPart("fname", new StringBody(user_fname));
            entity.addPart("lname", new StringBody(user_lname));
            entity.addPart("file", new ByteArrayBody(data, "picture.jpg"));
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost,
                    localContext);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));

            String sResponse = reader.readLine();
            return sResponse;

and php is

         $fname = $_POST["fname"];
         $lname = $_POST["lname"];
         $date_of_birth = $_POST["dob"];
         $pnumber = $_POST["pnumber"];
         $district_name = $_POST["district_name"];
         $gender = basename($_POST['gender']);
         $picture =  md5(basename($_FILES['file']['name'])).".jpg";

Where could I be going wrong

1

There are 1 answers

0
Asad Ullah Rafiq On
FileBody uploadVideo = new FileBody(new File(filePath));
     MultipartEntity reqEntity = new MultipartEntity();
     reqEntity.addPart( "file", uploadVideo );
     try {
        reqEntity.addPart( "ToId", new StringBody(contactId));
        reqEntity.addPart( "type", new StringBody("1"));
        reqEntity.addPart( "MsgText", new StringBody(""));
        reqEntity.addPart( "FromId", new StringBody(pref.getUserId()));
        reqEntity.addPart( "UniqueId", new StringBody(ControllerActivity.DEVICE_UNIQUE_ID));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

You only need to use FileBody instead of