尝试打开WebAPI的套接字时,我一直收到此错误。我已经测试了WebAPI,并能够使用我的RESTED查询它,并取得了成功。我的服务器也没有抛出任何错误。我真的很感谢所有帮助我们解决问题的人。
我已经对我的AppManifest文件具有与Web相关的所有权限,但尚未解决。我尝试了所有重新启动Android Studio和模拟器的方法,但这些方法均无效。
这是我的代码,实际上是在尝试打开套接字
private class PerformNetworkRequest extends AsyncTask<Void, Void, String>
{
String url;
HashMap<String, String> parameters;
int requestCode;
PerformNetworkRequest(String url, HashMap<String, String> parameters, int requestCode)
{
this.url = url;
this.parameters = parameters;
this.requestCode = requestCode;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
//progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
//progressBar.setVisibility(GONE);
try
{
JSONObject object = new JSONObject(s);
if(!object.getBoolean("error"))
{
Toast.makeText(getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
}
}
catch(JSONException e)
{
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids)
{
RequestHandler requestHandler = new RequestHandler();
if(requestCode == CODE_POST_REQUEST)
{
return requestHandler.sendPostRequest(url, parameters);
}
if(requestCode == CODE_GET_REQUEST)
{
return requestHandler.sendGetRequest(url);
}
return null;
}
}
在下面,我附加了上面的代码从中调用方法的requestHandler类,以尝试将GET和POST请求实际发送到PHP webAPI。
public class RequestHandler
{
public String sendPostRequest(String requestURL, HashMap<String, String> postDataParameters)
{
URL url;
StringBuilder builder = new StringBuilder();
try
{
//Create connection
url = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//Configure the properties of our connection
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
//Create an output stream from out connection so we can parse it
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParameters));
writer.flush();
writer.close();
os.close();
int responseCode = connection.getResponseCode();
if(responseCode == HttpsURLConnection.HTTP_OK)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
builder = new StringBuilder();
String response;
while((response = reader.readLine())!= null)
{
builder.append(response);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return builder.toString();
}
public String sendGetRequest(String requestURL)
{
StringBuilder builder = new StringBuilder();
try
{
URL url = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String s;
while((s = reader.readLine()) != null)
{
builder.append(s + "\n");
}
}
catch(Exception e)
{
}
return builder.toString();
}
//Returns the data in a POST String for parsing later.
private String getPostDataString(HashMap<String, String> parameters) throws UnsupportedEncodingException
{
StringBuilder builder = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry: parameters.entrySet())
{
if(first)
{
first = false;
}
else
{
builder.append("&");
}
builder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
builder.append("=");
builder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return builder.toString();
}
我已在Android Studio中附加了Logcat中的错误,希望对您有用。
2019-06-11 17:13:04.726 22716-24424/com.example.project W/System.err: java.net.SocketException: socket failed: EPERM (Operation not permitted)
2019-06-11 17:13:04.726 22716-24424/com.example.project W/System.err: at java.net.Socket.createImpl(Socket.java:492)
2019-06-11 17:13:04.726 22716-24424/com.example.project W/System.err: at java.net.Socket.getImpl(Socket.java:552)
2019-06-11 17:13:04.726 22716-24424/com.example.project W/System.err: at java.net.Socket.setSoTimeout(Socket.java:1180)
2019-06-11 17:13:04.727 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
2019-06-11 17:13:04.753 1948-2154/? D/AudioFlinger: mixer(0xf349f7c0) throttle end: throttle time(8)
2019-06-11 17:13:04.754 1948-2154/? D/AF::Track: interceptBuffer: took 1034us to intercept 0 tracks
2019-06-11 17:13:04.763 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
2019-06-11 17:13:04.763 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
2019-06-11 17:13:04.763 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
2019-06-11 17:13:04.763 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
2019-06-11 17:13:04.763 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
2019-06-11 17:13:04.763 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
2019-06-11 17:13:04.764 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
2019-06-11 17:13:04.764 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
2019-06-11 17:13:04.764 22716-24424/com.example.project W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262)
2019-06-11 17:13:04.764 22716-24424/com.example.project W/System.err: at com.example.project.RequestHandler.sendPostRequest(RequestHandler.java:36)
2019-06-11 17:13:04.764 22716-24424/com.example.project W/System.err: at com.example.project.RegisterActivity$PerformNetworkRequest.doInBackground(RegisterActivity.java:153)
2019-06-11 17:13:04.764 22716-24424/com.example.project W/System.err: at com.example.project.RegisterActivity$PerformNetworkRequest.doInBackground(RegisterActivity.java:106)
2019-06-11 17:13:04.765 22716-24424/com.example.project W/System.err: at android.os.AsyncTask$3.call(AsyncTask.java:378)
2019-06-11 17:13:04.765 22716-24424/com.example.project W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-06-11 17:13:04.765 22716-24424/com.example.project W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
2019-06-11 17:13:04.765 22716-24424/com.example.project W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2019-06-11 17:13:04.774 22716-24424/com.example.project W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2019-06-11 17:13:04.779 22716-24424/com.example.project W/System.err: at java.lang.Thread.run(Thread.java:919)