简体   繁体   中英

Passing data between android and server using JSON parsing

I'm trying to send and receive data between android app and server using JSON but unfortunately i am unable to do it i searched a lot but it didn't help. My code is as follows:

In android

try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fromDate", from);
        jsonObject.put("toDate", to);
        jsonObject.put("reason", reas);
        new SendData().execute(jsonObject);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

and then my asynctask 's doInBackground method

try {
                HttpParams httpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParams,
                        CmsInter.TIMEOUT);
                HttpConnectionParams.setSoTimeout(httpParams, CmsInter.TIMEOUT);
                HttpClient client = new DefaultHttpClient(httpParams);
                HttpPost httpPost = new HttpPost(getResources().getString(
                        R.string.URL));
                StringEntity se = new StringEntity(object.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                httpPost.setEntity(se);
                HttpResponse response = client.execute(httpPost);
                message = Sync.convertResponseToString(response);
            } catch (Exception e) {
                e.printStackTrace();
                message = e.toString();
            }

and then on server side within the doPost() method of servlet code is as follows :

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
            StringBuffer sb = new StringBuffer();
            String line = null;
            try {
                BufferedReader reader = request.getReader();
                while ((line = reader.readLine()) != null)
                    sb.append(line);
            } catch (Exception e) { /* report an error */
                e.printStackTrace();
            }
            String str = sb.toString();
            try {
                org.json.JSONObject json = new org.json.JSONObject(str);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}

and I think this is not the right way to read JSONObject on the server side because when I run this I get org.json.JSONException: A JSONObject text must begin with '{' at character 1 exception , Can anyone please help me out in this. Any help will be appreciated. Thanks.

终于明白了这一行是错误的object.toString()发送时应该是object[0].toString()因为asynctask的doInBackground方法参数是一个数组,即protected String doInBackground(JSONObject... object)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM