Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按下按钮时不显示任何内容_Android_Android Button_Androidhttpclient_Android Json - Fatal编程技术网

Android 按下按钮时不显示任何内容

Android 按下按钮时不显示任何内容,android,android-button,androidhttpclient,android-json,Android,Android Button,Androidhttpclient,Android Json,我正在开发一个android应用程序,其想法是当你按下按钮时,应用程序将连接到一个数据库,并显示数据库内容。这里的问题是什么都不会发生 这是onclick方法 Button butt1=(Button)findViewById(R.id.buttonUN); butt1.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-g

我正在开发一个android应用程序,其想法是当你按下按钮时,应用程序将连接到一个数据库,并显示数据库内容。这里的问题是什么都不会发生

这是onclick方法

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();
            }
     }
});
这里是我的php文件:

<?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();
?>

通过查看您的代码,应用程序将崩溃,因为您正在主线程上执行网络操作。将HTTP请求放在单独的线程或异步任务中。@KennyC我应该怎么做?创建单独的线程或异步任务@你说的HTTP请求是什么意思?哪个方法将i放入线程?HttpGet,即HTTP请求,方法为GET。有各种方法,张贴,放置,删除等。在android中,如果您通过网络发出任何请求,例如HTTP请求,则它不能位于主线程上。也就是说,您必须将它放在一个单独的线程中,或者创建一个异步任务,它在后台线程中为您工作。因此,将所有涉及请求PHP脚本的代码放在单独的线程或异步任务中。