我有一个使用visual studio构建的api,当我运行它时,它运行IIS Express。 我还有一个Android应用程序,我使用Android Studio构建。 Android应用程序用于访问api并返回JSON响应,但我获得了权限被拒绝的异常。
java.net.SocketException: Permission denied
我的Android代码是:
public class MainActivity extends AppCompatActivity {
Button btnGET;
final String API = "http://localhost:1337/Test/b";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGET = (Button) findViewById(R.id.btnGET);
btnGET.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DoGetTestTable().execute();
}
});
}
class DoGetTestTable extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try{
URL url = new URL(API);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
InputStream is = httpURLConnection.getInputStream();
int byteCharacter;
String result = "";
while((byteCharacter = is.read()) != -1){
result += (char) byteCharacter;
}
Log.d("json api", result);
} catch (Exception e){
e.printStackTrace();
}
return null;
}
}
}
我的追踪是:
07-01 22:59:31.736 2429-2846/sentzke.jackson.apitest W/System.err: java.net.SocketException: Permission denied
07-01 22:59:31.736 2429-2846/sentzke.jackson.apitest W/System.err: at java.net.Socket.createImpl(Socket.java:454)
07-01 22:59:31.736 2429-2846/sentzke.jackson.apitest W/System.err: at java.net.Socket.getImpl(Socket.java:517)
07-01 22:59:31.736 2429-2846/sentzke.jackson.apitest W/System.err: at java.net.Socket.setSoTimeout(Socket.java:1108)
07-01 22:59:31.736 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.Connection.connectSocket(Connection.java:195)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.Connection.connect(Connection.java:172)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
07-01 22:59:31.737 2429-2846/sentzke.jackson.apitest W/System.err: at sentzke.jackson.apitest.MainActivity$DoGetTestTable.doInBackground(MainActivity.java:42)
07-01 22:59:31.738 2429-2846/sentzke.jackson.apitest W/System.err: at sentzke.jackson.apitest.MainActivity$DoGetTestTable.doInBackground(MainActivity.java:34)
07-01 22:59:31.738 2429-2846/sentzke.jackson.apitest W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304)
07-01 22:59:31.738 2429-2846/sentzke.jackson.apitest W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-01 22:59:31.738 2429-2846/sentzke.jackson.apitest W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
07-01 22:59:31.739 2429-2846/sentzke.jackson.apitest W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
07-01 22:59:31.739 2429-2846/sentzke.jackson.apitest W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
07-01 22:59:31.739 2429-2846/sentzke.jackson.apitest W/System.err: at java.lang.Thread.run(Thread.java:761)
我已尝试将本地主机更改为10.0.2.2:1337,但这也无效。 有什么建议吗?
答案 0 :(得分:0)
在Android上当然应该使用10.0.2.2:1337,但在IIS Express方面也需要进行更改,将主机名从“localhost”更改为“127.0.0.1”,或者为空。
<binding protocol="http" bindingInformation="*:7265:127.0.0.1" />
您可以在我的博文中找到所有详细信息,
https://blog.lextudio.com/how-to-let-android-emulator-access-iis-express-f6530a02b1d3