Json parse error android!!!


Last Updated:

  1. niccloud

    niccloud New Member This Topic's Starter

    Joined:
    Sep 29, 2010
    Messages:
    3
    Likes Received:
    0
    Hi, i am new to this forum. I have also just started on android programming. I seem to be stuck a point where i cannot seem to solve.
    This happened when i was trying to call data from an online database using Json. I was hoping anyone could enlighten me on this issue. Thanks, the source code is below.
    Code (Text):
    1.  
    2. package com.mysql;
    3.  
    4. import java.io.BufferedReader;
    5. import java.io.InputStream;
    6. import java.io.InputStreamReader;
    7. import java.util.ArrayList;
    8.  
    9. import org.apache.http.HttpEntity;
    10. import org.apache.http.HttpResponse;
    11. import org.apache.http.NameValuePair;
    12. import org.apache.http.client.HttpClient;
    13. import org.apache.http.client.entity.UrlEncodedFormEntity;
    14. import org.apache.http.client.methods.HttpPost;
    15. import org.apache.http.impl.client.DefaultHttpClient;
    16. import org.apache.http.message.BasicNameValuePair;
    17. import org.json.JSONArray;
    18. import org.json.JSONException;
    19. import org.json.JSONObject;
    20.  
    21. import android.app.Activity;
    22. import android.os.Bundle;
    23. import android.util.Log;
    24.  
    25. public class mysql extends Activity {
    26.     private static final InputStream is = null;
    27.  
    28.     /** Called when the activity is first created.
    29.      * @param is */
    30.     @SuppressWarnings("null")
    31.     @Override
    32.     public void onCreate(Bundle savedInstanceState) {
    33.         super.onCreate(savedInstanceState);
    34.         setContentView(R.layout.main);
    35.        
    36.         String result = "";
    37.  
    38.         //the year data to send
    39.         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    40.         nameValuePairs.add(new BasicNameValuePair("location","Tampines"));
    41.         //http post
    42.         try{
    43.                 HttpClient httpclient = new DefaultHttpClient();
    44.                 HttpPost httppost = new HttpPost("http://www.niccloud.byethost12.com/GetMall.php");
    45.                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    46.                 HttpResponse response = httpclient.execute(httppost);
    47.                 HttpEntity entity = response.getEntity();
    48.                 InputStream is = entity.getContent();
    49.  
    50.         }catch(Exception e){
    51.                 Log.e("log_tag", "Error in http connection "+e.toString());
    52.         }
    53.         //convert response to string
    54.         try{
    55.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    56.                 StringBuilder sb = new StringBuilder();
    57.                 String line = null;
    58.                 while ((line = reader.readLine()) != null) {
    59.                         sb.append(line + "\n");
    60.                 }
    61.                 is.close();
    62.          
    63.                 result=sb.toString();
    64.         }catch(Exception e){
    65.                 Log.e("log_tag", "Error converting result "+e.toString());
    66.         }
    67.          
    68.         //parse json data
    69.         try{
    70.                 JSONArray jArray = new JSONArray(result);
    71.                 for(int i=0;i<jArray.length();i++){
    72.                         JSONObject json_data = jArray.getJSONObject(i);
    73.                         Log.i("log_tag","_id"+json_data.getInt("_id")+
    74.                                 ", mall_name"+json_data.getString("mall_name")+
    75.                                 ", location"+json_data.getString("location")+
    76.                                 ", telephone"+json_data.getString("telephone")+
    77.                                 ", area"+json_data.getString("area")+
    78.                                 ", email"+json_data.getString("email")
    79.                         );
    80.        
    81.                 }
    82.        
    83.         }catch(JSONException e){
    84.                 Log.e("log_tag", "Error parsing data "+e.toString());
    85.         }
    86.  
    87.     }
    88. }
    89.  
    The error message log is written below.
    09-30 06:54:13.285: ERROR/log_tag(223): Error converting result java.lang.NullPointerException
    09-30 06:54:13.295: ERROR/log_tag(223): Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 0 of
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Please place your code in CODE tags... It is too much of a strain on the eyes to read your code in its current format...

    EDIT: But by reading the last two lines of your post (the Logcat extract), I can tell you that your request sent to the PHP file is returning null.
     
  3. niccloud

    niccloud New Member This Topic's Starter

    Joined:
    Sep 29, 2010
    Messages:
    3
    Likes Received:
    0
    Thanks for the reply, it seems that the PHP file was in fact returning a 'NULL' value. This problem seems to be solved on the online server as seen from this link.
    HTML:
    1. http://www.niccloud.byethost12.com/GetMall.php
    However, i still seem to be getting a similar error below, could you enlighten me as to where i am going wrong?
    10-05 04:50:24.513: ERROR/log_tag(213): Error converting result java.lang.NullPointerException
    10-05 04:50:24.523: ERROR/log_tag(213): Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 0 of
     

Share This Page

Loading...