0

I am posting JSON Array to a webservice but i am unable to solve it. In the below code "str" is the JSONArray converted to string. JSONArray consists of JSONObjects and each JSONObject consists of 3 paramaters (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"));

this is the error what i am getting

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)

I want to pass below JSONArray to 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"
                        }]

Thanks in Advance.

Naveen
  • 814
  • 2
  • 9
  • 22

2 Answers2

0

try this code:

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

        } 
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
0

org.json.JSONException: End of input at character 0 of

First no need to append \n char at end of every line when reading json String. just use :

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

And second important mistake is parsing String as JSONObject, JSON string contains JSONArray as root item instead of JSONObject, so convert result to JSONArray.change:

JSONObject json_data = new JSONObject(result);

to

JSONArray json_data = new JSONArray(result);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks for responding @ρяσѕρєя K. I got same error after changing to JSONArray. Actually here i want to pass JSONArray and get JSONObject as response from webservice. – Naveen May 06 '16 at 09:45
  • @Naveen: Why not using `HttpURLConnection ` instead of `HttpClient ` because Apache HTTP Client is removed in latest version – ρяσѕρєя K May 06 '16 at 09:48
  • i have referenced this link http://stackoverflow.com/questions/13134019/http-post-method-passing-null-values-to-the-server/13134287#13134287. I was struck here from past 2 days. – Naveen May 06 '16 at 09:51
  • @Naveen: see http://www.xyzws.com/javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139 example for using HttpURLConnection. change your code using HttpURLConnection and let me know if getting any issue – ρяσѕρєя K May 06 '16 at 09:55
  • In the above link, can i pass jsonarray as parameter ? @ρяσѕρєя K – Naveen May 06 '16 at 09:59
  • @Naveen: yes you can pass same String – ρяσѕρєя K May 06 '16 at 10:00
  • I am getting this error when i am using the above link... 05-06 15:47:58.599: W/System.err(12458): java.io.FileNotFoundException: http://172.16.10.64:8080/plugleadservices/rest/feedbackmanagement/feedbacknew 05-06 15:47:58.599: W/System.err(12458): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206) 05-06 15:47:58.599: W/System.err(12458): at com.plugleads.feedback.FeedBackQuestionsActivity.excutePost(FeedBackQuestionsActivity.java:870) – Naveen May 06 '16 at 10:30