java.net.SocketTimeoutException:无法连接到(我的IP)

时间:2016-04-27 14:45:35

标签: java android mysql

我正在开发一个需要连接到mysql的Android应用程序。我使用我的电脑作为localhost托管wampserver。我通过我的电脑将我真正的Android设备连接到mysql。使用我的家庭网络与IP 192.168.1.x连接时,连接正常,我的应用程序在检索和插入数据时工作正常。但是,当我使用其他网络/ IP地址连接时,例如10.207.x.x或192.168.0.x,它会抛出错误。我已经检查过我已经添加了INTERNET权限,我尝试在连接时更改端口号。 http://192.168.0.1:8081/(php文件目录)但它仍然无法正常工作。有任何帮助,我已经挣扎了好几天。

错误日志:

04-27 22:19:23.920 4644-4740/? W/System.err: java.net.SocketTimeoutException: failed to connect to /192.168.0.108 (port 8085) after 15000ms
04-27 22:19:23.940 4644-4740/? W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
04-27 22:19:23.940 4644-4740/? W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
04-27 22:19:23.950 4644-4740/? W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-27 22:19:23.960 4644-4740/? W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
04-27 22:19:23.960 4644-4740/? W/System.err:     at java.net.Socket.connect(Socket.java:833)
04-27 22:19:23.970 4644-4740/? W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:152)
04-27 22:19:23.980 4644-4740/? W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:101)
04-27 22:19:23.980 4644-4740/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
04-27 22:19:23.990 4644-4740/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
04-27 22:19:24.000 4644-4740/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
04-27 22:19:24.000 4644-4740/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
04-27 22:19:24.010 4644-4740/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
04-27 22:19:24.020 4644-4740/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
04-27 22:19:24.030 4644-4740/? W/System.err:     at com.qualcomm.vuforia.samples.SampleApplication.utils.HttpUrlConnectionParser.makehttpUrlConnection(HttpUrlConnectionParser.java:48)
04-27 22:19:24.030 4644-4740/? W/System.err:     at com.qualcomm.vuforia.samples.Books.app.Books.FeedBack$ConnectDatabase.doInBackground(FeedBack.java:182)
04-27 22:19:24.040 4644-4740/? W/System.err:     at com.qualcomm.vuforia.samples.Books.app.Books.FeedBack$ConnectDatabase.doInBackground(FeedBack.java:140)
04-27 22:19:24.050 4644-4740/? W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-27 22:19:24.050 4644-4740/? W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-27 22:19:24.060 4644-4740/? W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-27 22:19:24.070 4644-4740/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-27 22:19:24.070 4644-4740/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-27 22:19:24.080 4644-4740/? W/System.err:     at java.lang.Thread.run(Thread.java:841)

我的doInBackground代码:

 @Override
    protected String doInBackground(String... params) {
        String fb_url = "http://192.168.0.108:8085/android/comment.php";
        String get_list = "http://192.168.0.108:8085/android/get_list.php";
        String get_id = "http://192.168.0.108:8085/android/get_id.php";
        data = new HashMap<>();
        String[] response = new String[]{"Response Accepted, Thanks!","Leave us some comment =)"};
        String method = params[0];
        if (method.equals("feedBack")) {

            String id = params[1];
            String bus = params[2];
            String depart_station = params[3];
            String arrive_station = params[4];
            String item = params[5];
            String comment = params[6];

            data.put("reported_case",id);
            data.put("reported_bus",bus);
            data.put("depart_station", depart_station);
            data.put("arrive_station", arrive_station);
            data.put("reported_item", item);
            data.put("reporter_comment", comment);

            try {
                String result = HttpUrlConnectionParser.makehttpUrlConnection(fb_url, data);
                result = null;
                return response[0];

            } catch (Exception e) {
            }

        } else if (method.equals("getList")) {

            try {
                String result =     HttpUrlConnectionParser.makehttpUrlConnection(get_list, data);
                JSONObject json = new JSONObject(result);
                boolean ssucc = json.getBoolean("ssuccess");
                boolean isucc = json.getBoolean("isuccess");
                boolean bsucc = json.getBoolean("bsuccess");

                if (!ssucc && !isucc &&!bsucc) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {
                    JSONArray jsonArrays = json.getJSONArray("Station");
                    JSONArray jsonArrayi = json.getJSONArray("Item");
                    JSONArray jsonArrayb = json.getJSONArray("BusType");
                    final List<String> lists = new ArrayList<>();
                    final List<String> listi = new ArrayList<>();
                    final List<String> listb = new ArrayList<>();


                    for (int i = 0; i < jsonArrayb.length(); i++) {
                        JSONObject child = jsonArrayb.getJSONObject(i);
                        String childBus = child.optString("Bus");
                        listb.add(childBus);

                    }

                    for (int i = 0; i < jsonArrays.length(); i++) {
                        JSONObject child = jsonArrays.getJSONObject(i);
                        String childId = child.optString("ID");
                        String childName = child.optString("Name");
                        lists.add(childId + " " + childName);
                    }

                    for (int i = 0; i < jsonArrayi.length(); i++) {
                        JSONObject child = jsonArrayi.getJSONObject(i);
                        String childId = child.optString("ID");
                        String childItem = child.optString("Item");
                        listi.add(childId + " " + childItem);

                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adp1 = new ArrayAdapter<>(getApplicationContext(),R.layout.spinner_text, listb);
                            adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            editBus.setAdapter(adp1);

                            ArrayAdapter<String> adp2 = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_text, listi);
                            adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            editItem.setAdapter(adp2);

                            ArrayAdapter<String> adp3 = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_text, lists);
                            adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            editStationDepart.setAdapter(adp3);
                            editStationArrive.setAdapter(adp3);
                        }
                    });

                }

                return response[1];

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if (method.equals("getId")) {

            try {
                String result = HttpUrlConnectionParser.makehttpUrlConnection(get_id, data);
                JSONObject json = new JSONObject(result);
                boolean fsucc = json.getBoolean("fsuccess");

                if (!fsucc) {
                    return null;
                }else{
                    JSONArray jsonArrayf = json.getJSONArray("FeedBack_id");
                    JSONObject child = jsonArrayf.getJSONObject(0);
                    String childId = child.optString("ID");

                    return childId;

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
            return null;

    }

httpParser类:

public static String makehttpUrlConnection(String   url_path,HashMap<String,String> data) {
    try {
        URL url = new URL(url_path);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setReadTimeout(10000);
        urlConnection.setConnectTimeout(20000);
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        OutputStream os = urlConnection.getOutputStream();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQueryData(data));
        writer.flush();
        writer.close();

        urlConnection.connect();
        is = urlConnection.getInputStream();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;


            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }


            json = sb.toString();
            is.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    return json;
}

0 个答案:

没有答案