java new socket 失败,java.net.SocketException:套接字失败:EPERM(不允许操作)

I am working on an Android Studio project with several activities. I am currently trying to read the output from a Java Servlet on localhost but it seems to be crashing due to a socket permission.

I've made a new project, used the exact same code and worked perfectly. So I dont understand why is not willing to work on my project.

public class LoginActivity extends AppCompatActivity {

String apiUrl = "http://10.0.2.2:8080/ProyectService/Servlet?action=login";

EditText username;

EditText password;

AlertDialog dialog;

Usuario session;

@Override

public void onCreate(Bundle savedInstanceState) {

// Inicializacion de ventana

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

getSupportActionBar().hide();

// Inicializacion de componentes

username = findViewById(R.id.username);

password = findViewById(R.id.password);

// Inicializacion de funcionalidad de botones

Button button= (Button) findViewById(R.id.login);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

UserLoginTask mAuthTask = new UserLoginTask();

mAuthTask.execute();

}

});

password = findViewById(R.id.password);

createAlertDialog("Usuario o Contraseña Incorrectos");

}

private void createAlertDialog(String message){

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage(message)

.setTitle("Error");

dialog = builder.create();

}

// ASYNCRONUS NETWORK PROCESS

public class UserLoginTask extends AsyncTask {

@Override

protected void onPreExecute() {

}

@Override

protected String doInBackground(String... params) {

// implement API in background and store the response in current variable

String current = "";

try {

URL url;

HttpURLConnection urlConnection = null;

try {

url = new URL(apiUrl);

System.out.println(apiUrl);

urlConnection = (HttpURLConnection) url

.openConnection();

InputStream in = urlConnection.getInputStream();

InputStreamReader isw = new InputStreamReader(in);

int data = isw.read();

while (data != -1) {

current += (char) data;

data = isw.read();

//System.out.print(current);

}

System.out.print(current);

// return the data to onPostExecute method

return current;

} catch (Exception e) {

e.printStackTrace();

} finally {

if (urlConnection != null) {

urlConnection.disconnect();

}

}

} catch (Exception e) {

e.printStackTrace();

return "Exception: " + e.getMessage();

}

return current;

}

}

protected void onPostExecute(String success) {

Log.i(success, "");

//attemptLogin();

}

}

I Expect it to read the data but it crashes at this line:

InputStream in = urlConnection.getInputStream();

This is the error output:

java.net.SocketException: socket failed: EPERM (Operation not permitted)

at java.net.Socket.createImpl(Socket.java:492)

at java.net.Socket.getImpl(Socket.java:552)

at java.net.Socket.setSoTimeout(Socket.java:1180)

at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)

at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)

at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)

at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)

at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)

at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)

at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)

at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)

at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)

at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)

at com.example.controller.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:114)

at com.example.controller.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:93)

at android.os.AsyncTask$3.call(AsyncTask.java:378)

at java.util.concurrent.FutureTask.run(FutureTask.java:266)

at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)

at java.lang.Thread.run(Thread.java:919)

解决方案

Solved: All I needed to do was to uninstall the app from the emulator and run it again.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值