【问题标题】:Post JSON Array to a webservice in android将 JSON 数组发布到 android 中的网络服务
【发布时间】:2016-05-06 09:27:39
【问题描述】:

我将 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"
                        }]

提前致谢。

【问题讨论】:

  • 712 FeedBackQuestionsActivity 中的行号是什么?
  • 712 - JSONObject json_data = new JSONObject(result);结果为空@ρяσѕρєяK

标签: android json post


【解决方案1】:

试试这个代码:

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);

        } 

【讨论】:

  • 这个答案如何帮助 OP 解决当前问题?
【解决方案2】:

org.json.JSONException: 在字符 0 处输入结束

读取json字符串时,首先不需要在每行末尾附加\n char。只需使用:

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

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

JSONObject json_data = new JSONObject(result);

JSONArray json_data = new JSONArray(result);

【讨论】:

  • 感谢您回复@ρяσѕρєя K。我在更改为 JSONArray 后遇到了同样的错误。实际上在这里我想传递 JSONArray 并获取 JSONObject 作为来自 web 服务的响应。
  • @Naveen:为什么不使用HttpURLConnection 而不是HttpClient ,因为Apache HTTP 客户端在最新版本中被删除了
  • 我已经引用了这个链接stackoverflow.com/questions/13134019/…。过去 2 天我在这里被击中。
  • @Naveen:请参阅xyzws.com/javafaq/… 使用 HttpURLConnection 的示例。使用 HttpURLConnection 更改您的代码,如果遇到任何问题,请告诉我
  • 在上面的链接中,我可以传递 jsonarray 作为参数吗? @ρяσѕρєяK
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多