如何使用POST方法向服务器发送的值的串阵列中的一个键字阵列、如何使用、服务器、方法

2023-09-04 04:01:24 作者:情毒

我用这code,但我不能够发送字符串数组:

 的HttpClient =新DefaultHttpClient();
httppost =新HttpPost(Call_Server.UPLOAD_VIDEO);
MultipartEntity mpEntity =新MultipartEntity(
        HttpMultipartMode.STRICT);
mpEntity.addPart(FILE_UPLOAD,新FileBody(videofile));
mpEntity.addPart(FROM_EMAIL,新StringBody(
        Call_Server.useremail));
mpEntity.addPart(recipient_emails,新StringBody(
        AddressArray));

httppost.setEntity(mpEntity);


HTT presponse响应= httpclient.execute(httppost);

HttpEntity resEntity = response.getEntity();
 

解决方案

您可以做这样的事情。

  // prepare类别阵
对于(字符串mBusinessID:mSelectedCategoryArray){
    reqEntity.addPart(CategoryCBG [],新StringBody(mBusinessID));
}
 
服务器阵列信息丢失,数据怎么办 先不要慌,也许很快就能恢复

只需添加 [] 与您数组变量和PaaS在这一个循环的值。

下面 CategoryCBG 是数组变量。您可以通过使用循环传递此标记你的价值观。将岗位作为服务器阵列。

下面是完整的code我如何使用的:

 公共字符串executeMultipartPost()抛出异常{
    尝试 {

        ByteArrayOutputStream mByteOutputStream =新ByteArrayOutputStream();

        mSelectedImage.com preSS(比较pressFormat.JPEG,75,mByteOutputStream);

        byte []的mImageByteDate = mByteOutputStream.toByteArray();

        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost postRequest =新HttpPost(CONSTANTS.MAIN_URL_GLOBAL + CONSTANTS.BUSINESS_REGISTRATION_TAG);

        ByteArrayBody mImageByteArray =新ByteArrayBody(mImageByteDate,Long.toString(System.currentTimeMillis的())+.JPG);

        MultipartEntity reqEntity =新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        reqEntity.addPart(logo_img,mImageByteArray);

        reqEntity.addPart(名,新StringBody(txtBusinessName_business_registration.getText()的toString()));
        reqEntity.addPart(电子邮件,新StringBody(txtBusinessEmail_business_registration.getText()的toString()));
        reqEntity.addPart(CONTACT_PHONE,新StringBody(txtBusinessPhone_business_registration.getText()的toString()));
        reqEntity.addPart(link_url,新StringBody(txtBusinessWebsite_business_registration.getText()的toString()));
        reqEntity.addPart(地址,新StringBody(txtStreetAddress_business_registration.getText()的toString()));
        reqEntity.addPart(城,新StringBody(txtCity_business_registration.getText()的toString()));
        reqEntity.addPart(国家,新StringBody(txtState_business_registration.getText()的toString()));
        reqEntity.addPart(邮编,新StringBody(txtZip_business_registration.getText()的toString()));
        reqEntity.addPart(详细信息,新StringBody(txtDetail_business_registration.getText()的toString()));
        reqEntity.addPart(产品,新StringBody(txtService_business_registration.getText()的toString()));

        // prepare类别阵
        对于(字符串mBusinessID:mSelectedCategoryArray){
            reqEntity.addPart(CategoryCBG [],新StringBody(mBusinessID));
        }

        postRequest.setEntity(reqEntity);
        HTT presponse响应= httpClient.execute(postRequest);
        的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),UTF-8));
        字符串sResponse;
        mStringBuilder =新的StringBuilder();

        而((sResponse = reader.readLine())!= NULL){
            mStringBuilder = mStringBuilder.append(sResponse);
        }

        返回mStringBuilder.toString();

    }赶上(例外五){
        e.printStackTrace();
        //这里处理异常
        Log.e(e.getClass()的getName(),e.​​getMessage());

        返回错误;


  }
    }
 

更新 要取消ongioing上传您可以使用

  httpClient.getConnectionManager()关闭();
 

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();

解决方案

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();

 
精彩推荐
图片推荐