Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

json - How to send JSONarray to server in Android using StringEntity?

I'm trying to send data to server from Android. There are some String values and a JSONarray called "multiples"

How I call the method

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

This is my the method to send data

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;

        }

    }

The response is an error code 500, internal server error. I don't know what is happining or what I'm doing wrong.

When I use BasicNameValuePair all data is sent except my "multiples" JSONArray

Please help. Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Convert JSONArray to String & Send it to Server

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...