1

私はEclipseを使用しており、MySQLデータベースをAndroidアプリケーションに接続したいと考えています. インターネットで見つけたコーディングを試してみましたが、エミュレータ モバイル デバイスに結果が表示されません。エラーも表示されません。私のMySQLデータベースに接続するための段階的な手順で私に返信してください.

public class MainActivity extends Activity {

        TextView resultview;
        private InputStream is;

        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            resultview=(TextView) findViewById(R.id.resultview);
        }

        public void getData() {
           String result = "";
            //the year data to send
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("name","suman"));

            //http post
            try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://localhost/hello/try.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(), "Connection 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(), "Input Reading 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(), " Input reading 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")+
                                ", Address: "+json_data.getInt("address")+
                                ", Contact: "+json_data.getInt("contact")
                        );
                        Toast.makeText(getApplicationContext(), "JsonArray pass", Toast.LENGTH_SHORT).show();
               }
            } catch(JSONException e) {
                    Log.e("log_tag", "Error parsing data "+e.toString());
                    Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
            }
        }
    }

PHPスクリプトは次のとおりです:-

     <?php
   mysql_connect("localhost","root");
    mysql_select_db("test");

   $q=mysql_query("SELECT * FROM tbl_user");
   while($e=mysql_fetch_assoc($q))
    $output[]=$e;

  print(json_encode($output));

  mysql_close();
  ?>
4

1 に答える 1

0

Android シミュレーターを実行しているコンピューターを参照する場合は、10.0.2.2代わりに IP アドレスを使用しますlocalhostここからもっと読むことができます

コードの変更で

http://localhost/hello/try.php

http://ip address/hello/try.php

于 2013-11-11T10:18:14.603 に答える