• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Json parse error android!!!

niccloud

Lurker
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:
package com.mysql;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class mysql extends Activity {
    private static final InputStream is = null;

    /** Called when the activity is first created. 
     * @param is */
    @SuppressWarnings("null")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String result = "";

        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("location","Tampines"));
        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.niccloud.byethost12.com/GetMall.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
        //convert response to string
        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());
        }
         
        //parse json data
        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","_id"+json_data.getInt("_id")+
                                ", mall_name"+json_data.getString("mall_name")+
                                ", location"+json_data.getString("location")+
                                ", telephone"+json_data.getString("telephone")+
                                ", area"+json_data.getString("area")+
                                ", email"+json_data.getString("email")
                        );
       
                }
        
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

    }
}
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
 
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.
 
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:
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
 
Back
Top Bottom