将JSON数组发布到android中的web服务

时间:2016-05-06 09:27:39

标签: android json post

我将JSON数组发布到web服务但我无法解决它。 在下面的代码" str"是JSONArray转换为字符串。 JSONArray由JSONObjects组成,每个JSONObject由3个参数组成(ID,Question,Answer)。

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("feedbackjson", str));
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/feedbacknew");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpParams httpParameters = new BasicHttpParams();
            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            //is = entity.getContent();

            result = EntityUtils.toString(entity).toString();


        } catch (Exception e) 
        {   
            Log.e("Loading Runnable Error in http connection  :", e.toString());
        }


        try 
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        }
        catch (Exception e) 
        {   
            Log.e("Loading Runnable Error converting result :", e.toString());
        }


        JSONObject json_data = new JSONObject(result);
        System.out.println("#### Status Message : " +json_data.getString("statusMessage").toString());
        System.out.println("#### Company ID : " +json_data.getInt("company_id"));

这是我得到的错误

05-06 14:50:48.459: W/System.err(8368): org.json.JSONException: End of input at character 0 of 
05-06 14:50:48.459: W/System.err(8368):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
05-06 14:50:48.459: W/System.err(8368):     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
05-06 14:50:48.459: W/System.err(8368):     at org.json.JSONObject.<init>(JSONObject.java:156)
05-06 14:50:48.459: W/System.err(8368):     at org.json.JSONObject.<init>(JSONObject.java:173)
05-06 14:50:48.459: W/System.err(8368):     at com.plugleads.feedback.FeedBackQuestionsActivity.callWebservice(FeedBackQuestionsActivity.java:712)
05-06 14:50:48.459: W/System.err(8368):     at com.plugleads.feedback.FeedBackQuestionsActivity.checkBoxes(FeedBackQuestionsActivity.java:371)
05-06 14:50:48.459: W/System.err(8368):     at com.plugleads.feedback.FeedBackQuestionsActivity.isValidData(FeedBackQuestionsActivity.java:350)
05-06 14:50:48.459: W/System.err(8368):     at com.plugleads.feedback.FeedBackQuestionsActivity$1.onClick(FeedBackQuestionsActivity.java:166)
05-06 14:50:48.459: W/System.err(8368):     at android.view.View.performClick(View.java:4785)
05-06 14:50:48.459: W/System.err(8368):     at android.view.View$PerformClick.run(View.java:19884)
05-06 14:50:48.460: W/System.err(8368):     at android.os.Handler.handleCallback(Handler.java:739)
05-06 14:50:48.460: W/System.err(8368):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-06 14:50:48.460: W/System.err(8368):     at android.os.Looper.loop(Looper.java:135)
05-06 14:50:48.460: W/System.err(8368):     at android.app.ActivityThread.main(ActivityThread.java:5343)
05-06 14:50:48.460: W/System.err(8368):     at java.lang.reflect.Method.invoke(Native Method)
05-06 14:50:48.460: W/System.err(8368):     at java.lang.reflect.Method.invoke(Method.java:372)
05-06 14:50:48.460: W/System.err(8368):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
05-06 14:50:48.460: W/System.err(8368):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

我想将JSONArray传递给Webservice

[
                {
                    "fk_Company_id":4,
                    "q_Answer":"no",
                    "q_Name":"qwhgdfqw"
            },
            {
                "fk_Company_id":2,
                "q_Answer":"yes",
                "q_Name":"sdfsdf"
                },
                {
                    "fk_Company_id":2,
                    "q_Answer":"yes",
                    "q_Name":"xcvfdgd"
                    },
                    {
                        "fk_Company_id":2,
                        "q_Answer":"no",
                        "q_Name":"xfgdf"
                        }]

先谢谢。

2 个答案:

答案 0 :(得分:0)

试试这段代码:

List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("dev_id", ID));
        params1.add(new BasicNameValuePair("Question", question));
        params1.add(new BasicNameValuePair("Answer", ans));
        cd = new ConnectionDetector(context);
        isInternetPresent = cd.isConnectingToInternet();
        if (isInternetPresent) {
            @SuppressWarnings("unused")
            JSONObject json = jsonParser.makeHttpRequest(url, "POST",
                    params1);

        } 

答案 1 :(得分:0)

  

org.json.JSONException:

的字符0处的输入结束

首先,在读取json String时,不需要在每行的末尾添加\n char。只需使用:

while ((line = reader.readLine()) != null) 
 {
    sb.append(line);
 }

第二个重要错误是将String解析为JSONObject,JSON字符串包含JSONArray作为根项而不是JSONObject,因此将result转换为JSONArray。改变:

JSONObject json_data = new JSONObject(result);

JSONArray json_data = new JSONArray(result);