user
user

Reputation: 631

connecting android application to database on the server?

Iam trying to make a connection between my database on the MYSQL(iam using the WAMP server) and android application , but when i run the application an exception is raised , i fixed the log cat and its points to this statement jArray = new JSONArray(result);

03-18 12:48:59.580: E/AndroidRuntime(458):  at org.json.JSONArray.<init>(JSONArray.java:87)

why this exception occur ?and how i can solve it ?

thanks in advanced..

this is my code : City.php:

          <?php
           mysql_connect("localhost","username","password");
           mysql_select_db("Deal");
           $sql=mysql_query("select * from City where Name like 'R%' ");
           while($row=mysql_fetch_assoc($sql))
           $output[]=$row;
           print(json_encode($output));
           mysql_close();
              ?>

the java class:

public class ConnectionActivity extends ListActivity {

int ct_id;
String[] ct_name = null;
JSONArray jArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;

    // http post
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://localhost/city.php");
                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);
                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();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            // paring data

            try {
                jArray = new JSONArray(result);
                JSONObject json_data = null;
                ct_name = new String[jArray.length()];
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    ct_id = json_data.getInt("City_ID");
                    ct_name[i] = json_data.getString("Name");
                }
            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG)
                        .show();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

            ListView lv;
            lv = getListView();
            lv.setTextFilterEnabled(true);
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ct_name);
            setListAdapter(adapter);



}
public void onListItemClick(ListView parent, View v, int position, long id)
{
        Toast.makeText(this,"You have selected " + ct_name[position],Toast.LENGTH_SHORT).show();
        }
}                      

log:

             03-18 13:28:46.923: W/ActivityThread(623): Application Com.Connection is waiting for the debugger on port 8100...
             03-18 13:28:46.973: I/System.out(623): Sending WAIT chunk
             03-18 13:28:46.983: I/dalvikvm(623): Debugger is active
             03-18 13:28:47.174: I/System.out(623): Debugger has connected
             03-18 13:28:47.183: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.383: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.583: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.783: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.993: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.193: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.403: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.603: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.803: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.003: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.203: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.413: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.613: I/System.out(623): debugger has settled (1497)
             03-18 13:30:17.593: E/log_tag(623): Error in http       connectionandroid.os.NetworkOnMainThreadException
               03-18 13:30:34.315: E/log_tag(623): Error converting result java.lang.NullPointerException

Upvotes: 0

Views: 766

Answers (1)

MByD
MByD

Reputation: 137442

You get an error as you try to perform HTTP connection on the main thread:

03-18 13:30:17.593: E/log_tag(623): Error in http       connectionandroid.os.NetworkOnMainThreadException

As far as I can see, the HTTP code is correct, but you are running it on the UI thread. This is not allowed. You need to perform such long actions on a background thread. Android gives you a very easy to use facilities to perform long action on the background and publish the results on the main (UI) thread. One of the most useful is AsyncTask. I would recommend that you read Painless Threading, as it will help you a lot IMO.

Upvotes: 1

Related Questions