java.net.SocketException: socket failed: EPERM (Operation not permitted) even after setting usesCleartextTraffic to true

huangapple 未分类评论43阅读模式
英文:

java.net.SocketException: socket failed: EPERM (Operation not permitted) even after setting usesCleartextTraffic to true

问题

问题出现在尝试使用Volley或httpURLConnection连接到Apache服务器时。我正在使用运行LAMP的DigitalOcean Ubuntu服务器,但这不应该成为问题,因为应用甚至无法连接到服务器。

我已经尝试过:

  • 卸载并重新安装应用程序
  • 在清单文件中添加 <uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 在清单文件中添加 android:usesCleartextTraffic="true"
  • 同时使用内置的Android Studio模拟器和我的手机(Google Pixel 4 XL)
  • 尝试以下所示的不同方法连接到服务器

我尝试使用Volley的第一种方法:##

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String success = jsonObject.getString("success");
                if(success.equals("1")) {
                    Toast.makeText(RegisterActivity.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(RegisterActivity.this, "Registration Error! Error inserting values to db" + e.toString(), Toast.LENGTH_SHORT).show();
                loading.setVisibility(View.GONE);
                registerButton.setVisibility(View.VISIBLE);
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            Toast.makeText(RegisterActivity.this, "Registration Error! error connecting to db " + error.toString(), Toast.LENGTH_SHORT).show();
            error.printStackTrace();
            loading.setVisibility(View.GONE);
            registerButton.setVisibility(View.VISIBLE);
        }
    })
{
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> params = new HashMap<>();
        params.put("name",name);
        params.put("emailAddress",emailAddress);
        params.put("password",password);
        params.put("postalAddress",postalAddress);
        params.put("phoneNumber",phoneNumber);
        params.put("bloodType",bloodType);

        return params;
    }
};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

使用httpURLConnection的第二种方法:##

// 将按钮设为不可见,显示加载小部件
loading.setVisibility(View.VISIBLE);
registerButton.setVisibility(View.GONE);

// 获取输入并转换为字符串
final String name = this.nameInput.getText().toString().trim();
final String emailAddress = this.emailAddressInput.getText().toString().trim();
final String password = this.passwordInput.getText().toString().trim();
// final String confirmPassword = this.confirmPasswordInput.getText().toString().trim();
final String postalAddress = this.addressInput.getText().toString().trim();
final String phoneNumber = this.nameInput.getText().toString().trim();
// final int phoneNumber = Integer.parseInt(this.nameInput.getText().toString().trim());
final String bloodType = bloodTypeInput.getSelectedItem().toString().trim();

class RegisterClass extends AsyncTask<String, Void, String> {

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

    @Override
    protected void onPostExecute(String httpResponseMsg) {

    }

    @Override
    protected String doInBackground(String... params) {
        hashMap.put("name", params[0]);
        hashMap.put("emailAddress", params[1]);
        hashMap.put("password", params[2]);
        hashMap.put("postalAddress", params[3]);
        hashMap.put("phoneNumber", params[4]);
        hashMap.put("bloodType", params[5]);

        String finalResult = httpParse.postRequest(hashMap, URL);

        return finalResult;
    }
}

RegisterClass registerClass = new RegisterClass();

registerClass.execute(name, emailAddress, password, postalAddress, phoneNumber, bloodType);

编辑

httpURLConnection的堆栈跟踪:##

W/System.err: 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)
    ...

Volley的堆栈跟踪:##

W/System.err: com.android.volley.NoConnectionError: java.net.SocketException: socket failed: EPERM (Operation not permitted)
    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:177)
    ...
英文:

The problem occurs when trying to connect to the apache server either using volley or httpURLConnection. I am using a digitalocean ubuntu server running LAMP but that shouldn't matter because the app isn't even connecting to the server.

I've tried:

  • uninstalling and reinstalling the app
  • adding &lt;uses-permission android:name=&quot;android.permission.INTERNET &quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
    to the manifest file
  • adding android:usesCleartextTraffic=&quot;true&quot; to the manifest file
  • using both the built in android studio emulator and my own phone (Google Pixel 4 XL)
  • trying different methods of connecting to the server as shown below

##The first way I tried using volley:##

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener&lt;String&gt;() {
                @Override
                public void onResponse(String response) {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString(&quot;success&quot;);
                        if(success.equals(&quot;1&quot;))
                        {
                            Toast.makeText(RegisterActivity.this, &quot;Registration Successful&quot;, Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(RegisterActivity.this, &quot;Registration Error! Error inserting values to db&quot; + e.toString(), Toast.LENGTH_SHORT).show();
                        loading.setVisibility(View.GONE);
                        registerButton.setVisibility(View.VISIBLE);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Toast.makeText(RegisterActivity.this, &quot;Registration Error! error connecting to db &quot; + error.toString(), Toast.LENGTH_SHORT).show();
                    error.printStackTrace();
                    loading.setVisibility(View.GONE);
                    registerButton.setVisibility(View.VISIBLE);
                }
            })
    {
        @Override
        protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
            Map&lt;String,String&gt; params = new HashMap&lt;&gt;();
            params.put(&quot;name&quot;,name);
            params.put(&quot;emailAddress&quot;,emailAddress);
            params.put(&quot;password&quot;,password);
            params.put(&quot;postalAddress&quot;,postalAddress);
            params.put(&quot;phoneNumber&quot;,phoneNumber);
            params.put(&quot;bloodType&quot;,bloodType);

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest); */

}

##The second way using httpURLConnection:##

//make button invisible and make loading widget visible
    loading.setVisibility(View.VISIBLE);
    registerButton.setVisibility(View.GONE);

    //get inputs and convert to strings
    final String name = this.nameInput.getText().toString().trim();
    final String emailAddress = this.emailAddressInput.getText().toString().trim();
    final String password = this.passwordInput.getText().toString().trim();
   // final String confirmPassword = this.confirmPasswordInput.getText().toString().trim();
    final String postalAddress = this.addressInput.getText().toString().trim();
    final String phoneNumber = this.nameInput.getText().toString().trim();
   // final int phoneNumber = Integer.parseInt(this.nameInput.getText().toString().trim());
    final String bloodType = bloodTypeInput.getSelectedItem().toString().trim();


    class RegisterClass extends AsyncTask&lt;String,Void,String&gt;{

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(String httpResponseMsg)
        {

        }
        @Override
        protected String doInBackground(String... params) {
            hashMap.put(&quot;name&quot;,params[0]);
            hashMap.put(&quot;emailAddress&quot;,params[1]);
            hashMap.put(&quot;password&quot;,params[2]);
            hashMap.put(&quot;postalAddress&quot;,params[3]);
            hashMap.put(&quot;phoneNumber&quot;,params[4]);
            hashMap.put(&quot;bloodType&quot;,params[5]);

            String finalResult = httpParse.postRequest(hashMap,URL);

            return finalResult;
        }
    }

    RegisterClass registerClass = new RegisterClass();

    registerClass.execute(name,emailAddress,password,postalAddress,phoneNumber,bloodType);

#edit#

##Stack trace for httpURLConnection:##

    W/System.err: 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)
W/System.err:     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.connect(HttpURLConnectionImpl.java:131)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262)
        at com.example.blooddonorsystem.HttpParse.postRequest(HttpParse.java:37)
        at com.example.blooddonorsystem.RegisterActivity$1RegisterClass.doInBackground(RegisterActivity.java:136)
        at com.example.blooddonorsystem.RegisterActivity$1RegisterClass.doInBackground(RegisterActivity.java:115)
        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)

##stack trace for volley:##

W/System.err: com.android.volley.NoConnectionError: java.net.SocketException: socket failed: EPERM (Operation not permitted)
    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:177)
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
Caused by: 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.connect(HttpURLConnectionImpl.java:131)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262)
    at com.android.volley.toolbox.HurlStack.addBody(HurlStack.java:259)
    at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:247)
    at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:219)
    at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:97)
    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
	... 2 more

huangapple
  • 本文由 发表于 2020年4月6日 02:41:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/61047647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定