连接机器人使用PHP到mysql机器人、PHP、mysql

2023-09-06 13:26:23 作者:吶街甪↘傷

我是一个初学者到Android。我正在试图将Android连接到MySQL。但在应用程序关闭unexceptedly.please解决我的问题:                     我想这$ C $下PHP连接到mysql。我使用的TextView布局file.Also我添加了 Internet权限在我的清单

我的code是:

 公共类AndroidConnectActivity扩展活动
     {
        私人的TextView的OutputStream;
        公共无效的onCreate(包savedInstanceState)
        {
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.activity_android_connect);
        JSONArray jArray;
        字符串结果= NULL;
        InputStream的是= NULL;
        StringBuilder的SB = NULL;
        的OutputStream =(TextView中)findViewById(R.id.hello_world);
        ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
        尝试
        {

            // HTTP POST
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(HTTP://localhost/android/index.php);
            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
            HTT presponse响应= httpclient.execute(httppost);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();
            如果(response.getStatusLine()的getStatus code()!= 200)
            {
                Log.d(MyApp的,服务器遇到了一个错误);
            }
        }
        赶上(例外五)
        {
            Toast.makeText(getBaseContext(),e.​​toString(),Toast.LENGTH_LONG).show();
        }

        //转换响应串
        尝试
        {
            的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是UTF8));
            SB =新的StringBuilder();
            sb.append(reader.readLine()+\ N);
            串线= NULL;

            而((行= reader.readLine())!= NULL)
            {
                sb.append(行+\ N);
            }
            is.close();
            结果= sb.toString();
        }
        赶上(例外五)
        {
              Log.e(log_tag,错误转换结果+ e.toString());
        }
        // END响应转换成字符串

        尝试
        {
            jArray =新JSONArray(结果);
            JSONObject的json_data = NULL;
            的for(int i = 0; I< jArray.length();我++)
            {
                json_data = jArray.getJSONObject(ⅰ);
                INT ID = json_data.getInt(ID);
                字符串名称= json_data.getString(姓名);
                outputStream.append(ID ++姓名+\ N);
            }
        }
        赶上(JSONException E1)
        {
            e1.printStackTrace();
        }
        赶上(ParseException的E1)
        {
            e1.printStackTrace();
        }
    }
}

< PHP
    的mysql_connect(localhost的根,);
    mysql_select_db(机器人);
    $ SQL =请求mysql_query(SELECT * FROM TABLE_1凡名称,比如'M%');
    而($行= mysql_fetch_assoc($ SQL))
    $输出[] = $行;
    打印(json_en code($输出));
    则mysql_close();
?>
 

解决方案

使用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 .

DW中怎么用PHP连接mysql

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