本地主机连接的Andr​​oid主机、Andr、oid

2023-09-04 04:03:26 作者:傲氣逼人 。

我想我的Andr​​oid应用程序连接到本地主机URL感谢WAMP的服务器,但它不工作。我的目标在这里,是为了获取JSON数据和分析这些数据。对于我的测试中,我使用的是设备而不是模拟器,我使用权限在AndroidManifest.xml中:

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
 

我的网址是这样的:

 字符串URL =htt​​p://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php;
 

我想:

 的http://本地主机/
http://10.0.2.2:8080/
http://10.0.2.2/
 

但它从来没有工作至今:

  java.net.ConnectException:无法连接到localhost / 127.0.0.1(80端口):连接失败:ECONNREFUSED(连接被拒绝)

    无法连接到/10.0.2.2(8080端口):连接失败:ETIMEDOUT(连接超时)

    java.net.ConnectException:无法连接到/10.0.2.2(80端口):连接失败:ETIMEDOUT(连接超时)
 

然后我试图用JSON URL测试在互联网上找到: http://headers.jsontest.com/

它的工作非常好,我在这个地址了JSON数据。所以我想我的code是好的,这里的问题是我的本地主机的网址,我不知道什么应该是它的确切形式.. 我读了很多关于它的线程,但我没有找到一个解决方案。

下面我code:

主要活动:

 公共类MainActivity延伸活动{
    私人字符串URL =htt​​p://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php;

    私人的ListView LV = NULL;
    私人按钮bGetData;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        最后JsonDownloaderTask任务=新JsonDownloaderTask(本);
        LV =(ListView控件)findViewById(R.id.list);

        bGetData =(按钮)findViewById(R.id.getdata);
        bGetData.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                task.execute(URL);
            }
        });
    }

    公共无效jsonTaskComplete(JSONArray数据){
        //去做
    }
}
 

AsyncTask的:

 公共类JsonDownloaderTask扩展的AsyncTask<字符串,字符串,JSONArray> {

    MainActivity马;

    公共JsonDownloaderTask(MainActivity主){
        MA =为主;
    }

    @覆盖
    保护JSONArray doInBackground(字符串... URL){
        JSONParser jParser =新JSONParser();
        //从URL获取JSON
        JSONArray jsonArray = NULL;
        尝试 {
            jsonArray = jParser.getJSONFromUrl(网址[0]);
        }赶上(IOException异常E){
            e.printStackTrace();
        }
        返回jsonArray;
    }

    保护无效onPostExecute(JSONArray数据){

        ma.jsonTaskComplete(数据);
    }
}
 

JSONParser:

 公共类JSONParser {
    字符串数据=;
    JSONArray jsonArray = NULL;
    InputStream的是= NULL;

    公共JSONParser(){}

    //方法下载网址的JSON数据
    公共JSONArray getJSONFromUrl(字符串strUrl)抛出IOException异常{
        尝试{
            网址URL =新的URL(strUrl);

            //创建一个HTTP连接,与URL沟通
            HttpURLConnection的的URLConnection =(HttpURLConnection类)url.openConnection();

            //连接给url
            urlConnection.connect();

            //从URL中读取数据
            是= urlConnection.getInputStream();

            的BufferedReader BR =新的BufferedReader(新InputStreamReader的(是));

            StringBuffer的SB =新的StringBuffer();

            串线=;
            而((行= br.readLine())!= NULL){
                sb.append(线);
            }
            is.close();
            数据= sb.toString();

            //br.close();

            jsonArray =新JSONArray(数据);

        }赶上(例外五){
            Log.d(异常边下载网址,e.toString());
        }最后{
            is.close();
        }

        返回jsonArray;
    }
}
 

解决方案

首先,你必须绑定在您的服务器在Eclipse设置运行的机器的IP地址。

您可以这样做。

右键点击在Eclipse,然后运行配置的 PHP 项目,然后在 Web应用程序在这里,你会发现在参数设置页。在其上运行服务器现在这里给你的计算机的端口和局域网的IP地址。

像这样的 - 端口= 8888 --address = 192.168.1.6 的然后更新网址 http://192.168.1.6:8080/tests/PhpProject1/ connectionBDD.php

在这里,在我的情况下,这是我的LAN IP地址192.168.1.6,然后您可以看到使用像 IPCONFIG 网​​络命令将其找到使用ifconfig ,并使用该IP地址。

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" />

My url looks like this :

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

i tried :

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)

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

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.

Here my code :

Main activity :

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
    }   
}

AsyncTask :

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);
    }
}

JSONParser :

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;
    }
}

解决方案

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

You can do this like this.

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.

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

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.