我可以使用Android模拟器访问我的笔记本电脑网络服务器,我使用10.0.2.2:portno
效果很好.
但是当我连接我真正的Android手机时,手机浏览器无法连接到笔记本电脑上的同一个Web服务器.手机通过USB线连接到笔记本电脑.如果我运行adb devices命令,我可以看到我的手机.
我错过了什么?
在对如何执行此操作感到困惑之后(如此处和此处所示),我现在使用以下代码成功连接到我的服务器应用程序和适当的 RESTful 方法:
public void onFetchBtnClicked(View v){
if(v.getId() == R.id.FetchBtn){
Toast.makeText(getApplicationContext(), "You mashed the button, dude.", Toast.LENGTH_SHORT).show();
new CallAPI().execute("http://10.0.2.2:28642/api/Departments/GetCount?serialNum=4242");
}
}
public static class CallAPI extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String urlString=params[0]; // URL to call
String resultToDisplay = "";
InputStream in = null;
// HTTP Get
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception e ) {
System.out.println(e.getMessage()); …Run Code Online (Sandbox Code Playgroud)