且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

连接机器人使用PHP到mysql

更新时间:2023-02-22 20:19:56

使用IP在 HTTP地址,而不是本地主机://localhost/android/index.php

I am a beginner to android .I am trying to connect android to mysql . but the application is closed unexceptedly.please fix my problem : I tried this code for connect php to mysql .I use textview in the layout file.Also i add the internet permission in my manifest .

My Code is :

   public class AndroidConnectActivity extends Activity 
     {
        private TextView outputStream;
        public void onCreate(Bundle savedInstanceState) 
        {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_android_connect);
        JSONArray jArray;
        String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        outputStream = (TextView)findViewById(R.id.hello_world);
        ArrayList<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>();
        try
        {

            //http post
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://localhost/android/index.php");
            httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            if (response.getStatusLine().getStatusCode() != 200) 
            {
                Log.d("MyApp", "Server encountered an error");
            }
        }
        catch(Exception e)
        {
            Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
        }

        //Convert response to string
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"));
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            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());
        }
        //END Convert response to string

        try
        {
            jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++)
            {
                json_data = jArray.getJSONObject(i);
                int id=json_data.getInt("id");
                String name=json_data.getString("name");
                outputStream.append(id +"" + name + "\n");
            }
        }
        catch(JSONException e1)
        {
            e1.printStackTrace();           
        } 
        catch (ParseException e1) 
        {
            e1.printStackTrace(); 
        }
    }
}

<?php 
    mysql_connect("localhost","root","");
    mysql_select_db("android");
    $sql=mysql_query("SELECT * FROM table_1 Where name like 'M%' ");
    while($row=mysql_fetch_assoc($sql))
    $output[]=$row;
    print(json_encode($output));
    mysql_close();
?>

Use ip address instead of localhost in http://localhost/android/index.php