0

i am trying to implement simple android application where i am trying to retrieve the database values over php site, but the following code generates error parsing json data.

my php file encoding json is as below

getdeals.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="user"; // Database name
$tbl_name="deal"; // Table name

// Connect to server and select databse.
mysql_connect("$host","$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$q=mysql_query("SELECT * FROM deal");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output));

mysql_close();
?>

table deal is as below table deal

android class is as below

public class JsonConnect extends Activity {

    InputStream is;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json_connect);
        addListenerOnbutton();
        Thread t = new Thread() {
            public void run() {
                getDeals();
            }
        };
        t.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_json_connect, menu);
        return true;
    }

    public void addListenerOnbutton() {

        final Context context = this;

        Button home = (Button)findViewById(R.id.button1);

        home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();  

            }

        });
 }

    public void getDeals() {

        String result = "";
        //the year data to send
        //ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //nameValuePairs.add(new BasicNameValuePair("year","1980"));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/dealnow/getdeals.php");
                //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                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","dealid: "+json_data.getString("deal_id")+
                                ", hotel name: "+json_data.getString("hotel_name")+
                                ", valid date: "+json_data.getInt("valid_date")+
                                ", location: "+json_data.getString("location")+
                                ", website: "+json_data.getString("website")
                        );
                }
        }
        catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }
}

and finally logcat output is

05-11 01:23:08.767: D/dalvikvm(435): GC_EXPLICIT freed 24K, 4% free 6582K/6855K, paused 7ms+4ms

05-11 01:29:24.164: V/TLINE(497): new: android.text.TextLine@4065aa30

05-11 01:29:24.757: D/dalvikvm(497): GC_CONCURRENT freed 115K, 4% free 6560K/6791K, paused 5ms+14ms

05-11 01:29:24.764: E/log_tag(497): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray

05-11 01:29:24.844: V/TLINE(497): new: android.text.TextLine@4065d8e0

05-11 01:34:12.147: D/dalvikvm(497): GC_EXPLICIT freed 48K, 4% free 6524K/6791K, paused 7ms+3ms

how can i get the desired values of the table

please help.

11
  • can you post the JSON Reponse? Commented May 10, 2013 at 20:17
  • @VishalPawale : can you tell me where to get it? Commented May 10, 2013 at 20:19
  • By JSON response, I mean the response returned by server/php script. In your case its result variable. Commented May 10, 2013 at 20:23
  • 2
    You need to put $output=array(); somewhere before the while loop. Commented May 10, 2013 at 20:34
  • 1
    @VishalPawale You should put your comment as the answer so OP can close the question. You did the work. Commented May 10, 2013 at 21:32

1 Answer 1

2

The response returned by PHP script is not in valid JSON format.

You need to put $output=array(); somewhere before the while loop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.