为什么我无法连接到网址?

时间:2017-01-27 11:42:44

标签: php android url

String log_url = "http://192.168.1.11/Rep/logini.php";

URL url = new URL(log_url);

HttpURLConnection connection =(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
OutputStream outputStream = connection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(nam, "UTF-8") + "&" + 
    URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(loc, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
connection.disconnect();

执行此代码时,我无法连接到此行url.Disconnecting中的OutputStream outputStream = connection.getOutputStream();

控制台中的消息如:

  

handlePacket:cmd = 0x1,cmdSet = 0xC7,len = 0x14,id = 0x400024DF,   flags = 0x0,dataLen = 0x9 01-27 16:59:55.848   12282-12289 / com.example.focture.medicalrep D / jdwp:sendBufferedRequest   :len = 0x34

2 个答案:

答案 0 :(得分:0)

看起来你正试图连接到你的......

如果这是您的IP 192.168.1.11 ,请改用 localhost

String log_url = "http://localhost/Rep/logini.php"; 

答案 1 :(得分:0)

检查

String post_data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(nam, "UTF-8") + "&" + 
    URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(loc, "UTF-8");
    URL obj = new URL("http://192.168.1.11/Rep/logini.php");
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(post_data.getBytes());
    os.flush();
    os.close();
    int responseCode = con.getResponseCode();
    System.out.println("POST Response Code :: " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) { //success
        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("POST request not worked");
    }