dongliuxia9495 2017-07-12 06:29
浏览 99

如何用android(PHP)连接到MySQL数据库?

I have tried almost all the code that have been encountered about this issue.

I leave sample code below.

//select.php
         <?php
$host='127.0.0.1';
$uname='root';
$pwd='';
$db="android";
$id=$_REQUEST['id'];
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
$sqlString = "select * from sample where id='$id'  ";
$rs = mysql_query($sqlString);  
if($rs){
while($objRs = mysql_fetch_assoc($rs)){
$output[] = $objRs; }
echo json_encode($output); }
mysql_close($con);
?>

//Main Activity

        @Override
        public void onClick(View v) {

        id=e_id.getText().toString();
        select();
        }
        });
        }

        public void select()    {
        ArrayList<NameValuePair> nameValuePairs = new 
        ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("id",id));

        try
        {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/select.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
        }
        catch(Exception e)
        {
        Log.e("Fail 1", e.toString());
        Toast.makeText(getApplicationContext(), "Invalid IP Address",
        Toast.LENGTH_LONG).show();
        }

        try
        {
        BufferedReader reader = new BufferedReader
        (new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
        sb.append(line + "
");
        }
        is.close();
        result = sb.toString();
        Log.e("pass 2", "connection success ");
        }
        catch(Exception e)
        {
        Log.e("Fail 2", e.toString());
        }

        try
        {
        JSONObject json_data = new JSONObject(result);
        name=(json_data.getString("name"));
        Toast.makeText(getBaseContext(), "Name : "+name,
        Toast.LENGTH_SHORT).show();
        }
        catch(Exception e)
        {
        Log.e("Fail 3", e.toString());
        }        }}

I wrote this to.

 <uses-permission android:name="android.permission.INTERNET"/>  

//logcat

   E/Fail 1: android.os.NetworkOnMainThreadException
 E/Fail 2: java.lang.NullPointerException: lock == null
 E/Fail 3: java.lang.NullPointerException

When I try to run the application I get the INVALID IP ADDRESS error. I need some suggestions. What should I try to connect to MySQL database with android (PHP)?

  • 写回答

3条回答 默认 最新

  • dsd30433 2017-07-12 06:39
    关注

    Exception clearly showing that you are calling network operation on main thread that's why it is not working . Use async task for network operation and then it will work. From api level 11 android restricted network operations on main thread and if do this it will throw an error network on main thread exception. And you are getting the same exception.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料