Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用post方法将一个关键字中的值的字符串数组发送到服务器_Java_Android - Fatal编程技术网

Java 如何使用post方法将一个关键字中的值的字符串数组发送到服务器

Java 如何使用post方法将一个关键字中的值的字符串数组发送到服务器,java,android,Java,Android,我正在使用此代码,但无法发送字符串数组: httpclient = new DefaultHttpClient(); httppost = new HttpPost(Call_Server.UPLOAD_VIDEO); MultipartEntity mpEntity = new MultipartEntity( HttpMultipartMode.STRICT); mpEntity.addPart("FILE_UPLOAD", new FileBody(videofile));

我正在使用此代码,但无法发送字符串数组:

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

你可以这样做

// Prepare Category Array
for (String mBusinessID : mSelectedCategoryArray) {
    reqEntity.addPart("CategoryCBG[]", new StringBody(mBusinessID));
}
只需在循环中添加
[]
数组标记和paas值

这里
CategoryCBG
是数组标记。您可以使用loop在该标记中传递值。它将作为一个数组发布到服务器上

下面是我如何使用它的完整代码:

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";


  }
    }
更新 要取消ongioing上载,您可以使用

httpClient.getConnectionManager().shutdown();

你可以这样做

// Prepare Category Array
for (String mBusinessID : mSelectedCategoryArray) {
    reqEntity.addPart("CategoryCBG[]", new StringBody(mBusinessID));
}
只需在循环中添加
[]
数组标记和paas值

这里
CategoryCBG
是数组标记。您可以使用loop在该标记中传递值。它将作为一个数组发布到服务器上

下面是我如何使用它的完整代码:

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";


  }
    }
更新 要取消ongioing上载,您可以使用

httpClient.getConnectionManager().shutdown();

谢谢你的重播。如何在上传之前取消上传?我已经更新了我的答案。请检查一下。如果此答案解决了您的问题,请将其标记为已接受答案。httpClient.getConnectionManager().shutdown();它工作得很好。但是当我喜欢的时候,它会抛出警告错误,我的数据只被覆盖并发送到最后一个数组元素,因为键是相同的。怎样才能避免呢?谢谢。但是必须用php在服务器端准备类别数组吗?谢谢你的重播。如何在上传之前取消上传?我已经更新了我的答案。请检查一下。如果此答案解决了您的问题,请将其标记为已接受答案。httpClient.getConnectionManager().shutdown();它工作得很好。但是当我喜欢的时候,它会抛出警告错误,我的数据只被覆盖并发送到最后一个数组元素,因为键是相同的。怎样才能避免呢?谢谢。但是必须用php在服务器端准备类别数组吗?