无法连接到Android Studio上的本地API [英] Impossible to connect to a local API on Android Studio
问题描述
因此,我正在开发一个Android应用程序,该应用程序必须与我编写的API进行交互.现在,我只在本地运行API.我已经测试了所有路线,并且都可以使用.
So I am developing an Android application that has to interact with an API I coded. For now I just run the API locally. I have tested all the routes already and they all work.
现在,我想在我的Android应用程序上向该API发送请求.为此,我使用扩展 AsyncTask
的类 RequestManager
来管理每个请求(如果您提出要求,我可以向您显示代码).我添加了< uses-permission android:name ="android.permission.INTERNET"/>
和< uses-permission android:name = android.permission.ACCESS_NETWORK_STATE"/>
在清单中.
Now I would like to send requests to this API on my Android application. For that I use a class RequestManager
that extends AsyncTask
to manage each request (I can show you the code if you ask).
I have added <uses-permission android:name="android.permission.INTERNET" />
and <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in the Manifest.
当我使用PC的IP地址通过 RequestManager
执行请求时,它会暂停一段时间,然后抛出 SocketTimeoutException
,并出现以下错误:无法从/XX.XX.XX.XX(端口XXXX)连接到/XX.XX.XX.XX(端口XXXX)
.请注意,向Postman发出相同的请求不会有任何问题.
When I execute a request throught the RequestManager
using the IP address of my PC, it pauses for some time then throws a SocketTimeoutException
with the following error: failed to connect to /XX.XX.XX.XX (port XXXX) from /XX.XX.XX.XX (port XXXX)
. Note that making the same request from Postman works without issue.
因此,我尝试了多种操作,例如添加文件 network_security_config.xml
以允许使用PC的IP地址进行通信,我停用了防火墙,在防火墙上添加了一些入站和出站规则,授予Android Studio的权限,IP地址,使用的端口等.但是似乎无法解决此问题.
So I tried multiple things like adding a file network_security_config.xml
to permit the traffic with the IP address of my PC, I deactivated my firewall, I added several inbound and outbound rules on my firewall to give permissions to Android Studio, the IP address, the used port etc.. but nothing seems to fix the issue..
有人经历过同样的经历吗?有人可以帮助我解决此问题吗?我真的需要让它工作..
Has anyone experienced the same or can anyone help me fix this ? I really need to get it working..
这是RequestManager类:
Here is the RequestManager class:
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进行测试.
use localtunnel to expose your local api temporarily. This works like a charm and is very easy to use. have used it for multiple projects to test against local API.
这篇关于无法连接到Android Studio上的本地API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!