Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

java - How to send the string array of values in one key word using post method to the server

I am using this code but i am not able to send the string array:

httpclient = new DefaultHttpClient();
httppost = new HttpPost(Call_Server.UPLOAD_VIDEO);
MultipartEntity mpEntity = new MultipartEntity(
        HttpMultipartMode.STRICT);
mpEntity.addPart("FILE_UPLOAD", new FileBody(videofile));
mpEntity.addPart("from_email", new StringBody(
        Call_Server.useremail));
mpEntity.addPart("recipient_emails", new StringBody(
        AddressArray));

httppost.setEntity(mpEntity);


HttpResponse response = httpclient.execute(httppost);

HttpEntity resEntity = response.getEntity();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do something like this.

// Prepare Category Array
for (String mBusinessID : mSelectedCategoryArray) {
    reqEntity.addPart("CategoryCBG[]", new StringBody(mBusinessID));
}

Just add [] with you array tag and paas values in a loop in it.

Here CategoryCBG is the array tag. You can pass your values in this tag by using loop. it will post as an array on server.

Here is the complete code how I used it :

public String executeMultipartPost() throws Exception {
    try {

        ByteArrayOutputStream mByteOutputStream = new ByteArrayOutputStream();

        mSelectedImage.compress(CompressFormat.JPEG, 75, mByteOutputStream);

        byte[] mImageByteDate = mByteOutputStream.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(CONSTANTS.MAIN_URL_GLOBAL + CONSTANTS.BUSINESS_REGISTRATION_TAG);

        ByteArrayBody mImageByteArray = new ByteArrayBody(mImageByteDate, Long.toString(System.currentTimeMillis()) + ".jpg");

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        reqEntity.addPart("logo_img", mImageByteArray);

        reqEntity.addPart("name", new StringBody(txtBusinessName_business_registration.getText().toString()));
        reqEntity.addPart("email", new StringBody(txtBusinessEmail_business_registration.getText().toString()));
        reqEntity.addPart("contact_phone", new StringBody(txtBusinessPhone_business_registration.getText().toString()));
        reqEntity.addPart("link_url", new StringBody(txtBusinessWebsite_business_registration.getText().toString()));
        reqEntity.addPart("Address", new StringBody(txtStreetAddress_business_registration.getText().toString()));
        reqEntity.addPart("City", new StringBody(txtCity_business_registration.getText().toString()));
        reqEntity.addPart("State", new StringBody(txtState_business_registration.getText().toString()));
        reqEntity.addPart("Zip", new StringBody(txtZip_business_registration.getText().toString()));
        reqEntity.addPart("details", new StringBody(txtDetail_business_registration.getText().toString()));
        reqEntity.addPart("products", new StringBody(txtService_business_registration.getText().toString()));

        // Prepare Category Array
        for (String mBusinessID : mSelectedCategoryArray) {
            reqEntity.addPart("CategoryCBG[]", new StringBody(mBusinessID));
        }

        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        mStringBuilder = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            mStringBuilder = mStringBuilder.append(sResponse);
        }

        return mStringBuilder.toString();

    } catch (Exception e) {
        e.printStackTrace();
        // handle exception here
        Log.e(e.getClass().getName(), e.getMessage());

        return "error";


  }
    }

Update To cancel ongioing uploading you can use

httpClient.getConnectionManager().shutdown();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...