Проблема возникает при попытке подключения к серверу apache с использованием volley или httpURLConnection. Я использую сервер digitalocean ubuntu, работающий под LAMP, но это не должно иметь значения, потому что приложение даже не подключается к серверу.
Я пробовал:
- удаление и повторная установка приложение
- добавление
<uses-permission android:name="android.permission.INTERNET " />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
к файлу манифеста - добавление
android:usesCleartextTraffic="true"
к файлу манифеста - с использованием встроенного эмулятора студии android и моего телефона (Google Pixel 4 XL)
- пробует различные способы подключения к серверу, как показано ниже
Первый способ, которым я попытался использовать залп:
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:
//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<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);
edit
Трассировка стека для 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)
трассировка стека для залпа:
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