【问题标题】:Connect android device locally by using a web service使用 Web 服务在本地连接 android 设备
【发布时间】:2017-06-25 15:52:01
【问题描述】:

我是原生 android 开发的新手。我使用xampp 服务器在phpmyadmin 中创建了一个DB。然后我使用Yii 创建了一个REST webservice。我在ARC 上测试了网络服务,它运行良好。现在我想在我的设备上测试它。所以我搜索了很多文章并找到了答案(link)。但这并没有帮助我。下面是我的代码

public class MainActivity extends AppCompatActivity {

String URLGET = "http://192.168.8.85:8000/app/web/users/";
String result = "";
TextView getData;
EditText userInput;
public static final String LOG_TAG = MainActivity.class.getSimpleName();

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


    getData = (TextView) findViewById(R.id.getData);
    userInput = (EditText) findViewById(R.id.EnterId);

    final Button button = (Button) findViewById(R.id.btn_Submit);


    button.setOnClickListener(new Button.OnClickListener() {


        @Override
        public void onClick(View v) {
            String reqURL = URLGET;

            new RestOperation().execute(reqURL);
            //callWebService(query);
        }
    });

}

public class RestOperation extends AsyncTask<String, Void, Void> {


    final HttpClient httpClient = new DefaultHttpClient();
    String content;
    String error;
    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    String data = "";

   // TextView getData = (TextView) findViewById(R.id.getData);
   // EditText userInput = (EditText) findViewById(R.id.EnterId);


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog.setTitle("Please Wait...");
        progressDialog.show();

        data = "data=" + URLEncoder.encode(String.valueOf(userInput.getText()));
    }

    @Override
    protected Void doInBackground(String... params) {

        BufferedReader br = null;

        URL url = null;
        try {
            url = new URL(params[0]);


            URLConnection connection = url.openConnection();

            connection.setDoOutput(true);

            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());

            outputStreamWriter.write(data);
            outputStreamWriter.flush();

            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append(System.getProperty("line.separator"));


            }

            content = sb.toString();

        } catch (MalformedURLException e) {
            error = " Exception: " + e.getMessage();
            e.printStackTrace();
        } catch (IOException e) {
            error = " IOException: " + e.getMessage();
            e.printStackTrace();
        } finally {
            try {
                if(br != null)
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        progressDialog.dismiss();

        if (error != null) {
            getData.setText("Error" + error);
        } else {
            getData.setText(content);


        }
    }
}}

上面的ip地址是我的无线地址。首先,我在模拟器上使用localhost 对其进行测试,但在搜索时我发现我应该使用10.0.2.2。所以我使用它,它仍然无法正常工作。我在logcat 中给了我一个warning,如下所示

我一定错过了一些我不知道的东西。我坚持了将近 2-3 天,真的不知道该怎么办。

任何帮助将不胜感激

【问题讨论】:

  • 您的设备和计算机应该在同一个网络中。使用该网络的 IP 地址并关闭防火墙。
  • 好吧,我来告诉你
  • 我按照你说的做了我运行了这个应用程序,这个错误显示在java.net.ConnectException: failed to connect to /192.168.8.85 (port 8000): connect failed: EHOSTUNREACH (No route to host)
  • 在浏览器中点击 URL 检查它是否正常工作。
  • 不,不是,它只适用于localhost

标签: android android-studio yii xampp


【解决方案1】:

打开您的 Android 手机/路由器的 Wifi 热点并将您的笔记本电脑连接到您的手机。

在 localhost 启动你的服务器(我正在使用 wamp 服务器)

现在打开命令提示符并输入 ipconfig 命令您将得到以下内容

Wireless LAN adapter Wireless Network Connection:
  Connection-specific DNS Suffix  . :
  Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11
  IPv4 Address. . . . . . . . . . . : **192.168.43.76**
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 192.168.43.1

将此192.168.43.76复制到您的移动浏览器中。

注意:请将您的网络设置为“家庭网络”。设置家庭网络意味着允许您的 PC 与同一网络上的其他设备共享内容。 如果您使用的是 Windows 10,请转到 WiFi > 当前网络的网络属性 > 使这台 PC 可发现。

原答案:How can I access my localhost from my Android device?

【讨论】:

  • Turn on Wifi Hotspot of your Android phone/router 是什么意思?我是否必须打开WifiWifi Hotspot 我猜这两个是不同的
  • 这个答案仅供参考。您可以使用WiFi Hotspot of your android deviceWiFi。只需确保您的设备和计算机在同一网络中,XAMPP/WAPP 正在运行,防火墙已关闭。
  • 好的,我可以在网络浏览器中看到结果,但是我想访问我的应用程序,因为我已经发布了屏幕截图。
  • 现在也检查设备。
  • java.io.FileNotFoundException: http://192.168.8.65:8000/app/web/users/ :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 2013-03-04
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 2014-02-10
  • 2016-10-01
相关资源
最近更新 更多