Android 连接到本地主机

2022-01-25 00:00:00 json wamp localhost android java

感谢 wamp 服务器,我正在尝试将我的 android 应用程序连接到本地主机 url,但它不起作用.我的目标是获取 json 数据并解析这些数据.对于我的测试,我使用的是不是模拟器的设备,并且我在 AndroidManifest.xml 中使用了权限:

I'm trying to connect my android application to a local host url thanks to wamp server but it doesn't work. My goal here, is to fetch json data and parse these data. For my test, i'm using a device not the emulator and i use permission in AndroidManifest.xml :

<uses-permission android:name="android.permission.INTERNET" />

我的网址是这样的:

String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";

我试过了:

http://localhost/
http://10.0.2.2:8080/
http://10.0.2.2/

但到目前为止它从未起作用:

But it never worked so far :

    java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)

    failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)

    java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)

然后我尝试了在互联网上找到的 json url 测试:http://headers.jsontest.com/

Then i tried with a json url test found on the internet : http://headers.jsontest.com/

效果非常好,我在这个地址获得了 json 数据.所以我想我的代码很好,这里的问题是我的 localhost url,我不知道它的确切形式应该是什么..我阅读了很多关于它的主题,但我没有找到解决方案.

It worked really good and i got json data at this address. So i guess my code is good and the issue here is my localhost url, i don't know what should be its exact form.. I read many threads about it but i didn't find a solution.

这是我的代码:

主要活动:

public class MainActivity extends Activity {
    private String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";

    private ListView lv = null;
    private Button bGetData;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final JsonDownloaderTask task = new JsonDownloaderTask(this);
        lv = (ListView) findViewById(R.id.list);

        bGetData = (Button)findViewById(R.id.getdata);
        bGetData.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {               
                task.execute(url);                      
            }
        });
    }

    public void jsonTaskComplete(JSONArray data){
        //todo
    }   
}

异步任务:

public class JsonDownloaderTask extends AsyncTask<String, String, JSONArray> {

    MainActivity ma;

    public JsonDownloaderTask(MainActivity main){
        ma = main;
    }

    @Override
    protected JSONArray doInBackground(String... url) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONArray jsonArray = null;
        try {
            jsonArray = jParser.getJSONFromUrl(url[0]);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonArray;        
    }

    protected void onPostExecute(JSONArray data){

        ma.jsonTaskComplete(data);
    }
}

JSON解析器:

public class JSONParser {
    String data = "";
    JSONArray jsonArray = null;        
    InputStream is = null;

    public JSONParser(){}

    // Method to download json data from url
    public JSONArray getJSONFromUrl(String strUrl) throws IOException{
        try{
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            is = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            StringBuffer sb  = new StringBuffer();

            String line = "";
            while( ( line = br.readLine())  != null){
                sb.append(line);
            }
            is.close();
            data = sb.toString();

            //br.close();

            jsonArray = new JSONArray(data);

        }catch(Exception e){
            Log.d("Exception while downloading url", e.toString());
        }finally{
            is.close();
        }

        return jsonArray;
    }
}

推荐答案

首先你要在eclipse设置中绑定你的服务器所在机器的IP地址.

First you have to bind the IP address of the machine where your server is running in the eclipse settings.

你可以这样做.

右键单击 Eclipse 中的 PHP 项目,然后运行配置,然后在 Web Application 中您将找到 Argument 选项卡.现在在这里给出运行服务器的机器的端口和 LAN IP 地址.

Right click on the PHP project in the eclipse then Run Configuration then In the Web Application where you will find the Argument tab. Now here give the port and LAN IP address of your machine on which your server is running.

类似这样的 --port=8888 --address=192.168.1.6 然后将 URL 更新为 http://192.168.1.6:8080/tests/PhpProject1/connectionBDD.php

Something like this --port=8888 --address=192.168.1.6 then update the URL to http://192.168.1.6:8080/tests/PhpProject1/connectionBDD.php

在我的情况下,这是我的 LAN IP 地址 192.168.1.6,您必须使用诸如 ipconfigifconfig 之类的网络命令找到它并使用该 IP地址.

Here in my case this is my LAN IP address 192.168.1.6, there you will have to find it using the network command like ipconfig , ifconfig and use that IP address.

相关文章