verbinden android-apps auf die mysql-Datenbank

Habe ich versucht aus dem tutorial gezeigt, die auf verschiedenen websites zum herstellen einer MySQL-Datenbank mit php auf android. Ich weiß nicht, was ist Los mit meinem code unten. Kann mir jemand sagen, was ich tun muss.

Dies ist mein php-code

 <?php
 mysql_connect("localhost","root","sugi");
 mysql_select_db("android");
 $q=mysql_query("SELECT * FROM people
 WHERE
 birthyear>'".$_REQUEST['year']."'");
 while($e=mysql_fetch_assoc($q))
             $output[]=$e;
       print(json_encode($output));
       mysql_close(); ?>

Dies ist meine sql-Abfrage

CREATE TABLE `people` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL ,
`sex` BOOL NOT NULL DEFAULT '1',
`birthyear` INT NOT NULL 
)

Dies ist mein java-code in android

public class main extends Activity {
    InputStream is;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String result = "";
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("year","1990"));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://localhost/index.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.e("log_tag", "connection success ");
                Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
                Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();

        }
        //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");
                        Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
                }
                is.close();

                result=sb.toString();
        }catch(Exception e){
               Log.e("log_tag", "Error converting result "+e.toString());
            Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();

        }

        //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","id: "+json_data.getInt("id")+
                                ", name: "+json_data.getString("name")+
                                ", sex: "+json_data.getInt("sex")+
                                ", birthyear: "+json_data.getInt("birthyear")
                        );
                        Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
               }

        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
                Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
        }
    }
}

Dem Programm gut arbeiten. aber ich kann keine Verbindung aufbauen zu http://localhost/index.php. Die Programm-Anzeige scheitern 3 mal. Kann mir helfen zu sehen, wo ich schief?

Danke allen für die Hilfe. Jetzt bin ich in der Lage, die mysql-Verbindung. Aber ich kann nicht den Wert von json-Daten. Das prog toast eine msg 2-pass und 1 Fehler. Kann mir jemand helfen? Das Bild unten ist wenn ich http://localhost/index.php in meinem IE. Und die Linie 6 ist all dies

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");

Ich weiß nicht, wo ich schief geht.

verbinden android-apps auf die mysql-Datenbank

Ich weiß nicht, etwas über PHP aber ich sehe, dass Sie die richtige Antwort auf Ihre Anfrage: array von JSON-Objekten. Wahrscheinlich php-Skript funktioniert normal.

InformationsquelleAutor sugianto | 2011-04-03

Schreibe einen Kommentar