因此,我正在开发必须与我编写的API交互的Android应用程序。现在,我只在本地运行API。我已经测试了所有路线,并且都可以使用。
现在,我想在Android应用程序上向该API发送请求。为此,我使用扩展了RequestManager
的类AsyncTask
来管理每个请求(如果您提出要求,我可以向您显示代码)。我在清单中添加了<uses-permission android:name="android.permission.INTERNET" />
和<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
。
[当我使用PC的IP地址通过RequestManager
执行请求时,它暂停了一段时间,然后抛出SocketTimeoutException
,但出现以下错误:failed to connect to /XX.XX.XX.XX (port XXXX) from /XX.XX.XX.XX (port XXXX)
。请注意,向Postman发出相同的请求也没有问题。
因此,我尝试了多种操作,例如添加文件network_security_config.xml
以允许使用PC的IP地址进行通信,我停用了防火墙,在防火墙上添加了一些入站和出站规则,以授予对Android Studio(IP)的权限地址,使用的端口等。但似乎无法解决此问题。.
有人经历过同样的经历吗?有人可以帮助我解决此问题吗?我真的需要让它工作。.
编辑:这是RequestManager类:
class RequestManager extends AsyncTask<HashMap<String, Object>, Void, Response> {
protected Response doInBackground(HashMap<String, Object>... parameterMaps) {
Response response = null;
HashMap<String, Object> params = parameterMaps[0];
String method = (String) params.get("method");
if ("GET".equals(method)) {
response = doGet(params);
} else if ("POST".equals(method)) {
response = doPost(params);
} else if ("UPDATE".equals(method)) {
response = doUpdate(params);
} else if ("DELETE".equals(method)) {
response = doDelete(params);
}
return response;
}
private Response doGet(HashMap<String, Object> params) {
Response response = null;
try {
OkHttpClient client = new OkHttpClient();
Context context = (Context) params.get("context");
String protocol = Config.getConfigValue(context, "protocol");
String ipAddress = Config.getConfigValue(context, "ipAddress");
String port = Config.getConfigValue(context, "port");
String route = (String) params.get("route");
String url = protocol + ipAddress + ":" + port + "/" + route;
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Content-Type", "application/json")
.addHeader("Cache-Control", "no-cache")
.build();
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
private Response doPost(HashMap<String, Object> params) {
Response response = null;
try {
OkHttpClient client = new OkHttpClient();
Context context = (Context) params.get("context");
String protocol = Config.getConfigValue(context, "protocol");
String ipAddress = Config.getConfigValue(context, "ipAddress");
String port = Config.getConfigValue(context, "port");
String route = (String) params.get("route");
String url = protocol + ipAddress + ":" + port + "/" + route;
MediaType mediaType = MediaType.parse("application/json");
String bodyContent = params.get("body").toString();
RequestBody body = RequestBody.create(mediaType, bodyContent);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Cache-Control", "no-cache")
.build();
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
private Response doUpdate(HashMap<String, Object> params) {
Response response = null;
try {
OkHttpClient client = new OkHttpClient();
Context context = (Context) params.get("context");
String protocol = Config.getConfigValue(context, "protocol");
String ipAddress = Config.getConfigValue(context, "ipAddress");
String port = Config.getConfigValue(context, "port");
String route = (String) params.get("route");
String url = protocol + ipAddress + ":" + port + "/" + route;
MediaType mediaType = MediaType.parse("application/json");
String bodyContent = params.get("body").toString();
RequestBody body = RequestBody.create(mediaType, bodyContent);
Request request = new Request.Builder()
.url(url)
.put(body)
.addHeader("Content-Type", "application/json")
.addHeader("Cache-Control", "no-cache")
.build();
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
private Response doDelete(HashMap<String, Object> params) {
Response response = null;
try {
OkHttpClient client = new OkHttpClient();
Context context = (Context) params.get("context");
String protocol = Config.getConfigValue(context, "protocol");
String ipAddress = Config.getConfigValue(context, "ipAddress");
String port = Config.getConfigValue(context, "port");
String route = (String) params.get("route");
String url = protocol + ipAddress + ":" + port + "/" + route;
Request request = new Request.Builder()
.url(url)
.delete()
.addHeader("Content-Type", "application/json")
.addHeader("Cache-Control", "no-cache")
.build();
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
protected void onPostExecute(Response response) {
}
}
这就是我的称呼:
HashMap<String, Object> params = new HashMap<>();
JSONObject body = Utils.jsonify(content);
params.put("body", body);
params.put("route", Constants.User.BASE_USER);
params.put("context", SignUp.this);
params.put("method", "POST");
AsyncTask<HashMap<String, Object>, Void, Response> requestManager = new RequestManager().execute(params);
使用localtunnel暂时公开您的本地api。这就像一个魅力,非常易于使用。已将其用于多个项目以针对本地API进行测试。