栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

你可以做这样的事情。

// Prepare Category Arrayfor (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";  }    }

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

httpClient.getConnectionManager().shutdown();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号