dongsong4418 2017-06-02 13:14
浏览 99

使用PhP在android和mysql之间建立连接时出错

I am creating a app which will show the data of MySQL table in the card_layout of the android app.

The below mentioned is the connection code in MainActivity.java

private void load_data_from_server(final int id) {
    AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        protected Void doInBackground(Integer... params) {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url("http://10.10.2.2/develop/php_recyler_demo.php?id="+id).build();
            try {
                Response response = client.newCall(request).execute();

                JSONArray array = new JSONArray(response.body().toString());

                for (int i=0; i<array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    MyData data = new MyData(object.getInt("id"),object.getString("description"),object.getString("image"));
                    data_list.add(data);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            adapter.notifyDataSetChanged();
        }
    };
    task.execute(id);
}

For the MySQL connection I am using PhP and executing in the XAMPP server as localhost, the code is as follows:

<?php
    $connection = mysqli_connect("localhost","root","","test");

    //$id = $_GET["id"];
    $id = isset($_GET['id']) ? $_GET['id'] : '';

    $query = "select * from recyclerdb where id between ($id+1) and ($id+4)";

    $result = mysqli_query($connection,$query);

    while ($row = mysqli_fetch_assoc($result)) {
        # code...
        $array[] = $row;
    }

    header('Content-type:Application/json');
    echo json_encode($array);
?>

My table structure is as follows: The table structure

When I run this PhP file using browser then I am getting correct output as JSON array:Image for the output of PhP file in browser

But if I execute my app in android studio using emulator

I am getting java.net.SocketTimeoutException: connect timed out

The problem I am getting at the two lines of MainActivity.java:

AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>()

Response response = client.newCall(request).execute();

I did research for a long time, please someone help me out.

  • 写回答

1条回答 默认 最新

  • duanlei8119 2017-06-02 13:17
    关注

    java.net.SocketTimeoutException: connect timed out means you just cannot connect to desired server.

    I guess for emulator you have to replace

    http://10.10.2.2/
    

    to

    localhost/
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题