0

Probably problem with the php file which contains the following code:

    <?php
mysql_connect("localhost","root","");
mysql_select_db("deal");
$sql=mysql_query("select * from CITY");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

simple code to retrieve all cities in db. I located this file in: C:\Program Files\EasyPHP-5.3.9\www\city.php

the java code is very simple, connecting and showing the query results:

package com.udios.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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MySQLActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onTestClick(View view) {
        /*Toast.makeText(MySQLActivity.this, "Test Pressed", Toast.LENGTH_SHORT)
                .show();*/

        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                String result = null;

                JSONArray jArray;
                InputStream is = null;
                StringBuilder sb = null;
                try {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://127.0.0.1/city.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "iso-8859-1"), 8);
                    sb = new StringBuilder();
                    sb.append(reader.readLine() + "\n");

                    String line = "0";
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();

                    int ct_id;
                    String ct_name;
                    Toast.makeText(getBaseContext(), "result is: "+result,
                            100).show();
                    jArray = new JSONArray(result);
                    JSONObject json_data = null;
                    for (int i = 0; i < jArray.length(); i++) {
                        json_data = jArray.getJSONObject(i);
                        ct_id = json_data.getInt("CITY_ID");
                        ct_name = json_data.getString("CITY_NAME");


                        Toast.makeText(getBaseContext(), Integer.toString(ct_id)+" : " +  ct_name , 
                                Toast.LENGTH_LONG).show();

                    }
                } catch (JSONException e1) {
                    Toast.makeText(getBaseContext(), "No City Found",
                            Toast.LENGTH_LONG).show();
                } catch (ParseException e1) {
                    e1.printStackTrace();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
        });

    }
}

When i'm trying to access directly in browser to:

I gets: Notice: Undefined variable: output in C:\Program Files\EasyPHP-5.3.9\www\city.php on line 7 null

i'm debugging in android and this line throws exception: (last catch)

HttpPost httppost = new HttpPost("http://127.0.0.1/city.php");

if i change it to 10.0.2.2 it throws exception in this line:

HttpResponse response = httpclient.execute(httppost);

i've really tried all combination, Can someone please help me? thank you very much udi

0

2 Answers 2

3

The first time you add a row to $output it is undefined.

Add this before the while loop in your PHP code.

$output = array();

You also need to connect 10.0.2.2 if you are running on the emulator, or your local IP on a real device (make sure the device is connected to your network via WiFi).

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

2 Comments

and use 10.0.2.2 to connect to your development machine's localhost.
You are correct. I stopped at the PHP error and didn't notice the problem with the IP address.
1

Your HttpPost is trying to connect the phone it is running on. It needs to point to the computer running your server. 127.0.0.1 = THIS DEVICE.

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.