0

I'm developing an android application, the idea is that when you press the button, the application will connect to a database and for a result, display the DB contents. The problem here is that nothing is coming.

This is the onclick() method

Button butt1=(Button)findViewById(R.id.buttonUN);
butt1.setOnClickListener( new OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),
            "chargement en cours", Toast.LENGTH_LONG).show();

        StringBuilder responseHTTP = new StringBuilder();//stocker la reponse HTTP
        HttpClient client =new DefaultHttpClient();      //envoi de requete HTTP

        HttpGet httpGet =new HttpGet                     //recuperer l'url de fichier PHP
            ("http://192.168.12.5/BaseUne/connect.php");     // via la methode HttpGet
            try{
                HttpResponse response= client.execute(httpGet); // recevoir la reponse
                StatusLine statusline=response.getStatusLine(); // StatusLine: methode pour savoir le statut de 
                int statuscode = statusline.getStatusCode();    // de la connexion
                if (statuscode==200){
                    HttpEntity entity =response.getEntity();
                    InputStream content = entity.getContent();   //InputStream :recuperer les données binaires
                    //InputStreamReader: construction des caracteres

                    BufferedReader reader= new BufferedReader(new InputStreamReader(content));
                    String line;
                    while((line=reader.readLine())!=null){
                        responseHTTP.append(line);
                    }
                    JSONObject jsonObject =new JSONObject(responseHTTP.toString());  // creation d'un objet JSON
                    int    ID       =jsonObject.getInt("ID_contenu");
                    String Descrip  =jsonObject.getString("Description");

                    Intent a =new Intent(ChoixDuSite.this,AdminInterface.class);

                    a.putExtra("String", Descrip);
                    a.putExtra("int", ID);

                    startActivity(a);

                }
            } catch (Exception e){
                Toast.makeText(getApplicationContext(),
                    e.getMessage(), Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
     }
});

Here my php file:

<?php 
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("DBname") or die(mysql_error());
$sql=mysql_query("SELECT * FROM TABLEname");
while($row=mysql_fetch_assoc($sql))
    print(json_encode($row));
mysql_close();
?>
Blo
  • 11,903
  • 5
  • 45
  • 99
  • Well by looking at your code, the app will crash because you are doing network on the main thread. Put the HTTP request in a separate thread or an Async Task. – KennyC Apr 13 '14 at 21:34
  • @KennyC what should i do ? –  Apr 13 '14 at 21:37
  • 1
    Create a separate thread or an AsyncTask. http://developer.android.com/reference/android/os/AsyncTask.html – KennyC Apr 13 '14 at 21:39
  • @KennyC what did you mean by HTTP request ? which method put i in thread ? –  Apr 13 '14 at 22:07
  • HttpGet, that is a HTTP request, the method being GET. There are various methods, POST,PUT,DELETE, etc... . In android, if you make any requests over the network (such as an HTTP request) it can not be on the main thread. Meaning, you MUST place it in a separate thread, or create an AsyncTask, which does work in a background thread for you. Soooo, place all of the code that involved making a request to your PHP script in a separate thread or an Asynctask. – KennyC Apr 13 '14 at 22:11
  • @KennyC like u said ! i putted all the code in this method : Thread net = new Thread(){ public void run(){ try{ }finally{ } } }; net.start(); and the result is the same : the Intent dosen't work and it's impossible to know if i receiverd the JSON attributs or not –  Apr 13 '14 at 22:49

0 Answers0