如何使用StringEntity将JSONarray发送到Android服务器?

时间:2017-03-01 22:13:02

标签: android json apache http parameters

我正试图从Android向服务器发送数据。有一些String值和一个名为"倍数"

的JSONarray

我如何调用方法

String id = new AddContactRequest().execute(token, etNameS, etLastNameS, etTitleS, etCompanyS, etUrlS, etDescriptionS, txtStatusS, "1", etAddressS, multiplesUpdatedArray.toString()).get();

这是我发送数据的方法

public class AddContact extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            String line;
            line = postData(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]);

            return line;

        }

        protected void onPostExecute(String result){
            super.onPostExecute(result);
        }

        protected void onProgressUpdate(Integer... progress){
            pb.setProgress(progress[0]);
        }

        public String postData(String token, String name, String lastname, String title, String company, String url, String description, String status, String campaign, String address, String multiples) {
        //public String postData(String token, String name, String lastname, String title, String company, String phone, String email, String url, String description, String status, String campaign, String address) {

            String line2 =  null;

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://fake.com/api/insert");

            try {
                JSONObject params = new JSONObject();
                params.put("token", token);
                params.put("name", name);
                params.put("lastname", lastname);
                params.put("title", title);
                params.put("company", company);
                params.put("url", url);
                params.put("description", description);
                params.put("address", address);
                params.put("status", status);
                params.put("campaign", campaign);
                params.put("multiples", multiples);

                httppost.setEntity(new StringEntity(params.toString(), HTTP.UTF_8));
                httppost.setHeader("Accept", "application/json");
                httppost.setHeader("Content-type", "application/json");

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity httpEntity = response.getEntity();
                line2 = EntityUtils.toString(httpEntity);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();

            } catch (MalformedURLException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return line2;

        }

    }

响应是错误代码500,内部服务器错误。我不知道什么是幸福或我做错了什么。

当我使用BasicNameValuePair时,除了我的&#34;倍数&#34;之外,所有数据都会被发送。 JSONArray

请帮忙。提前谢谢!

1 个答案:

答案 0 :(得分:2)

将JSONArray转换为String&amp;将其发送到服务器

StringEntity entity = new StringEntity(jsonArray.toString());