gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:48:50 26 4
gpt4 key购买 nike

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

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是数组标签。您可以使用循环在此标记中传递您的值。它将作为数组发布在服务器上。

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

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

关于java - 如何使用post方法将一个关键字中值的字符串数组发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16293388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com