anu_r
anu_r

Reputation: 1622

Why can't I retrieve data using JsonArray in Android?

I have inserted data into a server table called tsk. Now I want to retrieve it using JsonArray in my Android code. I used the following php code and I can see the parsed array on the webpage. The problem is that I am getting the error 04-04 01:36:18.198: E/log_tag(2254): Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray in the error log.

My database fields are as follows: tid is an Int set as a primary key and autoincremented. stat is a Text with length of the field set as 100. r is a VarChar with length set as 100.

My Json retrieval code in Android is as follows:

    String result = null;
    InputStream is = null;
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("myurl");
        ArrayList<NameValuePair>  nameValuePairs = new ArrayList<NameValuePair>();
       HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        Log.e("log_tag", "connection success ");

}
    catch(Exception e)
    {
            Log.e("log_tag", "Error in http connection "+e.toString());
            Toast.makeText(getActivity(), "Connection fail", Toast.LENGTH_SHORT).show();

    }
    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("log_tag", "Error converting result "+e.toString());
        Toast.makeText(getActivity(), " Input reading fail", Toast.LENGTH_SHORT).show();

    }
    try
    {
        JSONArray jArray = new JSONArray(result);
        String s="",s1,s2,s3,s4,s5;
        Log.w("Lengh",""+jArray.length());
          for(int i=0;i<jArray.length();i++){

            //  final ListView lview=(ListView) rootView.findViewById(R.id.listViewmytask);
            JSONObject json_data = jArray.getJSONObject(i);

                s=json_data.getString("stat");
                Log.i("taskid",""+s);
  }
  }

  catch(JSONException e)
    {
            Log.e("log_tag", "Error parsing data "+e.toString());
            Toast.makeText(getActivity(), "JsonArray fail", Toast.LENGTH_SHORT).show();
    }

JSON reesponse:

   [{"tid":"9","head":"task2","des":"task2.2","dateone":"2014\/4\/4 ","datetwo":"","timeone":"12:18","timetwo":"13:18","r":"123","s":"123456","stat":"not defined"},
    {"tid":"8","head":"task1","des":"task1.1","dateone":"2014\/4\/4 ","datetwo":"","timeone":"12:11","timetwo":"13:11","r":"12345","s":"123456","stat":"not defined"},
    {"tid":"10","head":"task3","des":"task3.3","dateone":"2014\/4\/3 ","datetwo":"2014\/5\/3 ","timeone":"13:25","timetwo":"13:26","r":"456","s":"123456","stat":"not defined"},    {"tid":"11","head":"task4","des":"task4.4","dateone":"2014\/3\/31 ","datetwo":"2014\/4\/30 ","timeone":"13:30","timetwo":"14:30","r":"123456","s":"123456","stat":"not defined"}]

The String Builder result is showing the following value: But it has some undefined characters in it like i`?(inverted question mark).

  04-04 01:44:04.965: I/result(2406): i`?[{"tid":"10","head":"task3","des":"task3.3","dateone":"2014-4-3 ","datetwo":"2014-5-3 ","timeone":"13:25","timetwo":"13:26","r":"456","s":"123456","stat":"not defined"}]

Upvotes: 0

Views: 121

Answers (2)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

change this code and try

StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8));
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

        } finally {
            is.close();
            reader.close();
        }
        result=sb.toString();

Upvotes: 1

Hashir Sheikh
Hashir Sheikh

Reputation: 1821

it's seems that server is not return jsonArray,make sure first letter of response , if it start { than it should be json object and if server return only '[]' this then server return empty data, which can't be parse into json array

Upvotes: 0

Related Questions