从mysql数据库中检索数据并在android应用程序中显示

时间:2012-10-12 08:18:57

标签: android mysql

我正在尝试从MySQL数据库中搜索数据并在我的Android应用程序中显示结果但是我遇到了困难。我已经尝试了所有方法但无济于事。没有显示任何错误所以我没有看到任何错误代码。 这是android活动代码:

public class search extends Activity implements OnClickListener{
        Button searchbutton;
        EditText identity;
        TextView details;
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.searchlayout);
            searchbutton=(Button)findViewById(R.id.button1search);
            identity=(EditText) findViewById(R.id.idnumber);
            details=(TextView)findViewById(R.id.tvresults);
            searchbutton.setOnClickListener(this);
        }
        public void onClick(View v) {

            String id=identity.getText().toString();
            if(id.trim().equals("")){
                String k="Please enter the id number";
                message(k);
        }
            else{
            switch(v.getId()){
            case R.id.idnumber:
                getServerData(identity.getText().toString());
                break;
            }

            }

        }   
        public static final String KEY_121 ="http://10.0.2.2/pk/androidCriminalSearch.php";
        public String data ="";

        private void getServerData(String get) {
                        String result="";
                        InputStream is = null;
                        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("id_number",identity.getText().toString()));
                    try{
                            HttpClient httpclient = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost(KEY_121);
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                            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());
                    }

                    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");
                        }
                        is.close();

                        result=sb.toString();
                }
                    catch(Exception e){
                        Log.e("log_tag", "Error converting result "+e.toString());
                }
                    try{
                        JSONArray jArray = new JSONArray(result);
                        for(int i=0;i<jArray.length();i++){
                                JSONObject json_data = jArray.getJSONObject(i);
                                Log.i("log_tag","Crime id: "+json_data.getInt("crime_id")+
                                        ", First name: "+json_data.getString("fname")+
                                        ", Last name: "+json_data.getString("lname")+
                                        ", Wanted for: "+json_data.getString("crime")+
                                        ", Location: "+json_data.getString("location")+
                                        ", Id number: "+json_data.getInt("id_no")
                                    );
                                data += "\n"+jArray.getString(i)+"\n";
                                details.setText(data);
                            }
                        }

                    catch(JSONException e){
                        Log.e("log_tag", "Error parsing data "+e.toString());
                    }

        }
        public void message(String msg){
            Context context=getApplicationContext();
            CharSequence text=msg;
            int duration=Toast.LENGTH_SHORT;
            Toast toast=Toast.makeText(context, text, duration);
            toast.show();
        }


    }

PHP代码如下所示

 <?php
    mysql_connect("localhost","root","");
    mysql_select_db("pk") or die (mysql_errno());
    $q=mysql_query("SELECT * FROM criminals WHERE id_no='".$_REQUEST['id_number']."'");
    while($e=mysql_fetch_assoc($q))
            $output[]=$e;
    print(json_encode($output));
     mysql_close();
    ?>

0 个答案:

没有答案