简体   繁体   English

Android,未连接到本地php文件

[英]Android, not connecting to local php file

I'm trying to have my android app execute code where it opens a URL connection to a local php file that returns database entries in JSON format, but it's not connecting, after commenting out the other lines I can see that it throws an exception at the lines: 我正在尝试让我的Android应用程序执行代码,在该代码中,它打开与本地php文件的URL连接,该连接以JSON格式返回数据库条目,但在注释了其他几行后,我没有看到连接,因此我看不到它线路:

connection = (HttpURLConnection) url.openConnection();
connection.connect();

Heres the screen shot of android code and the error log, the returned expected json file in windows and the php file: link 这是android代码和错误日志,在Windows中返回的预期json文件和php文件的屏幕截图: 链接

Can you change your 你能改变你的

URL url = new URL(getListUrl); 

into 进入

URL url = new URL("http://www.google.com");

and check the incoming data. 并检查传入的数据。

and the following is a working example. 以下是一个工作示例。

      try {
     URL url = new URL("http://www.google.com");
     URLConnection urlConn = url.openConnection();

     if (!(urlConn instanceof HttpURLConnection)) {
        throw new IOException("URL Exception");
     }
     HttpURLConnection httpConn = (HttpURLConnection) urlConn;
     httpConn.setAllowUserInteraction(false);
     httpConn.setInstanceFollowRedirects(true);
     httpConn.setRequestMethod("GET");
     httpConn.connect();
     resCode = httpConn.getResponseCode();

     if (resCode == HttpURLConnection.HTTP_OK) {
        in = httpConn.getInputStream();
     }
  }

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

My argument is if this code works for remote urls, it should work with localhost as well. 我的观点是,如果此代码适用于远程URL,则它也应与localhost一起使用。

Hi it must be that you are using wrong local ip address in android files it should be 10.0.2.2 not 127.0.0.1 嗨,一定是您在android文件中使用了错误的本地ip地址,它应该是10.0.2.2而不是127.0.0.1

First tell me are you testing on emulator or device 首先告诉我您要在模拟器或设备上进行测试

if emulator then where you define file path it should be http://10.0.2.2/request_entries.php 如果是模拟器,则在其中定义文件路径的位置应为http://10.0.2.2/request_entries.php

just like in code 就像在代码中一样

httpclient=new DefaultHttpClient();
                httppost= new HttpPost("http://10.0.2.2/request_entries.php"); 

Managed to get the input stream using the URL: 设法使用URL获取输入流:

URL url = new URL("http://10.0.2.2/request_entries.php");

result 结果

Thanks a bunch guys :) 谢谢一群人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM