0

我是新手,对android不太了解。我需要使用http post将用户定义类的arrayList发送到webservice。我可以使用 nameValuePairs 发送一个实例。但不知道如何在 android 中再发送 2 个相同的实例?这是我的代码

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        String[] array = params[0];
        String stringifiedResponse = null;
        HttpResponse response = null;
        for (int i = 0; i < array.length; i++) {

            nameValuePairs.add(new BasicNameValuePair("userid", share
                    .getUserId()));
            nameValuePairs.add(new BasicNameValuePair("email", array[i]));
            nameValuePairs
                    .add(new BasicNameValuePair("contact_number", ""));
            nameValuePairs.add(new BasicNameValuePair("name", ""));
        }
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.URL
                + "reward.php?status=add");
        String result = null;
        try {

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response = client.execute(post);
            stringifiedResponse = EntityUtils
                    .toString(response.getEntity());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.d("RESPONSE", "" + stringifiedResponse);

这仅发送我想发送所有条目的最后一个条目。请帮忙。

4

1 回答 1

0
use jackson library to create json string from array list of class object and after that send it. I have done successfully with this.


public String sendArrayListOfuserclass() {

        String request_string = null;
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
        HttpResponse response;
        try {
            HttpPost post = new HttpPost(
                    ServiceConstant.ALL_PERSON_UPDATE_DETAIL_URL);
            post.setHeader("Content-type", "application/x-www-form-urlencoded");
            DatabaseHelper mDatabaseHelper = DatabaseHelper
                    .getInstance(sContext);
            List<PersonInformationBean> allPersonDetailList = mDatabaseHelper
                    .getAllPersonDetail();
            List<ActivationBean2> PersonRequest = new ArrayList<ActivationBean2>();
            int size = allPersonDetailList.size();
            if (size > 0) {
                for (PersonInformationBean personInformationBean : allPersonDetailList) {
                    ActivationBean2 personactivationdata = new ActivationBean2();
                    try {
                        personactivationdata.setPersonID(personInformationBean
                                .getPersonId());
                        personactivationdata
                                .setActivationKey(personInformationBean
                                        .getActivationKey());
                    } catch (Exception e) {
                    }
                    PersonRequest.add(personactivationdata);
                }
            }
            RefreshPersonBean personwithdeviceid = new RefreshPersonBean();
            personwithdeviceid.setPersonRequest(PersonRequest);
            personwithdeviceid.setDeviceID(MyPreferences.getInstance(sContext)
                    .getDeviceId());
            request_string = convertBeanToJson(personwithdeviceid);
            request_string = request_string.replace("\\", "")
                    .replace("\"[", "[").replace("]\"", "]");
            request_string = request_string.replace("\n", "").replace("\r", "");
            Log.e("referesh person request", request_string);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair(
                    ServiceConstant.RequestData, request_string));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            // post.setEntity(new ByteArrayEntity(json.toString().getBytes()));
            response = client.execute(post);
            /* Checking response */
            if (response != null) {
                InputStream inputStream = response.getEntity().getContent(); // Get
                request_string = convertStreamToString(inputStream);
                Log.e("response",
                        "response of post = " + request_string.toString());
                return request_string;
            }
        } catch (IOException e) {
            request_string = "timeout";
            e.printStackTrace();
        } catch (Exception e) {
            // Toast.makeText(sContext, "Server Error",
            // Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
        return request_string;
    }



this is jackson library function:

private String convertBeanToJson(RefreshPersonBean bean) {
        String reqQuery = null;
        try {
            reqQuery = mapperObj.writeValueAsString(bean);
            Log.d("convertBeanToJson(): request query is:", reqQuery);
        } catch (JsonGenerationException e) {
            Log.e("convertBeanToJson():", " caught JsonGenerationException");
        } catch (JsonMappingException e) {
            // mELogger.error("convertBeanToJson(): caught JsonMappingException");
        } catch (IOException e) {
            Log.e("convertBeanToJson():", " caught IOException");
        }
        return reqQuery;
    }//

library name jackson-all-1.6.2
于 2013-07-01T14:17:36.090 回答