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 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线