如何从wampserver中的Android Emulator连接到我的http:// localhost Web服务器?

时间:2017-05-07 04:15:05

标签: android android-studio localhost

是的,这个问题已经被提出,我已经使用了之前问题的所有解决方案,但我没有找到解决方案。所以请不要标记为重复。

我可以在android studio中做什么,以便我的Android模拟器可以连接到http://localhost的localhost Web服务器页面?

我已经尝试了下面列出的所有解决方案,似乎无法让它发挥作用。

我尝试过的内容如下:

http://10.0.2.2/login.php

http://10.0.2.2:8080/login.php

http://10.0.3.2/login.php

使用我的IPv4地址

这就是我尝试连接本地主机的方式。

 String type = params[0];
        String login_url = "http://10.0.2.2 :80/login.php";
        if(type.equals("login")) {
            try {
                String user_name = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

0 个答案:

没有答案