我在我的 android 设备上使用 expo 客户端并设置了一个 django 服务器,localhost:8000我正在尝试在 react native 中使用 fetch 访问和端点。我看过如何从我的 Android 设备访问我的本地主机? 你如何在Android模拟器中连接本地主机?以及如何从 Eclipse 中的 Android Emulator 连接到我的 http://localhost Web 服务器,但这不起作用。
我也尝试使用我机器的 ip 地址而不是 localhost,但没有结果。这是一个示例代码
componentDidMount(){
return fetch('http://my-ip-address:8000/restapi/')
.then((response) => {
console.log("success")
response = response.json()
}).catch(error => {
console.error(error)
})
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
在电容模式下运行 Quasar 开发服务器quasar dev -m capacitor -T android结果出现在选择外部 IP 地址的对话框中:
? What external IP should Quasar use? 192.168.0.3\n App \xe2\x80\xa2 Updated src-capacitor/package.json\n App \xe2\x80\xa2 Updated capacitor.config.json\n App \xe2\x80\xa2 Running "...\\node_modules\\@capacitor\\cli\\bin\\capacitor sync android" in ...\nRun Code Online (Sandbox Code Playgroud)\n问题是,这会src-capacitor/capacitor.config.json使用可能意外推送的本地信息进行更新,这是永远不可取的:
"server": {\n "androidScheme": "https",\n "url": "http://192.168.0.3:9500" <-- this line is added\n }\nRun Code Online (Sandbox Code Playgroud)\n更不用说大多数时候跳过此对话框是很实用的,因为应用程序在本地运行并且可以安全地使用 localhost 而不是外部 IP。
\n正如文档中粗略描述的那样,这似乎是预期的行为,但不是理想的行为。
\n如何server.url避免更新 Capacitor.config.json?
我一直在尝试使用nativescript创建一个Android应用程序.我正在使用fetch模块从我的服务器获取响应.当我试图从httpbin.org/get获得响应时,它没关系.但是当我试图获得响应时从我的本地服务器,我收到网络请求失败.错误.
发送到httpbin.org/get-
return fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
console.log(r);
}, function (e) {
console.log(e);
});
Run Code Online (Sandbox Code Playgroud)
发送到localhost:8000/api-
return fetchModule.fetch("http://localhost:8000/api").then(response => { return response.text(); }).then(function (r) {
console.log(r);
}, function (e) {
console.log(e);
});
Run Code Online (Sandbox Code Playgroud)
当我尝试通过请求模块从localhost:8000/api获取纯node.js的响应.它工作正常.但是现在,我不知道如何使用fetch模块在nativescript中解决这个问题.
我目前正在开发一个使用android-studiowith的应用程序Kotlin和一个使用django-rest-framework.
我想127.0.0.1使用 android 模拟器访问我的本地服务器,以从 API 方法中获取一些值。
我在堆栈溢出网站和其他网站上检查了一些问题,并参考一些文章设置了我的代码,例如在 中添加一些代码、AndroidManifest.xml制作network_security_config.xml、更改 的代理Android emulator以及更改项目中的一些代码Django rest framework。
即使当我到达本地服务器中的端点时,我也会收到如下错误代码:
2020-10-07 22:37:39.318 7247-7247/com.sample.apitest I/System.out: checked
2020-10-07 22:37:39.358 7247-7280/com.sample.apitest D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2020-10-07 22:37:39.392 7247-7280/com.sample.apitest W/System.err: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8000
2020-10-07 22:37:39.394 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
2020-10-07 22:37:39.394 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
2020-10-07 22:37:39.394 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
2020-10-07 22:37:39.395 7247-7280/com.sample.apitest W/System.err: at …Run Code Online (Sandbox Code Playgroud) android localhost android-emulator django-rest-framework django-cors-headers
在我的应用程序中,我试图连接到具有指定主机的服务器(虚拟机),但是当我从本地计算机连接同一台服务器时,我不断收到错误消息“找不到具有指定主机名的服务器”通过浏览器或只是 ping,我能够做到这一点。我在本地机器上修改了主机文件,并在android模拟器上上传了相同的主机文件。有没有人有任何指示可以帮助我解决这个问题?
我试图JSONObject从数据库中检索一个,但我发现该请求甚至没有发送到服务器。我检查了Apache Access Log确认这一点。URL 有效,当我通过 Web 浏览器访问它时,它返回预期的 JSONObject。nullVolley 库显示的异常错误。
private void GetSchInfo(){
showDialog();
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
URL_SchInfo, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
spinnerClass.setVisibility(View.VISIBLE);
spinnerSch.setVisibility(View.VISIBLE);
spinnerDept.setVisibility(View.VISIBLE);
btnRegClass.setVisibility(View.VISIBLE);
txtClassMsg.setVisibility(View.VISIBLE);
btnRefresh.setVisibility(View.GONE);
JSONArray schools = response.getJSONArray("sch_info");
JSONArray departments = response.getJSONArray("dept_info");
for (int i = 0; i < schools.length(); i++){
JSONObject schObj = (JSONObject)schools.get(i);
SchInfo sch = new SchInfo(schObj.getString("school_id"), schObj.getString("school_name"));
schoolList.add(sch);
}
for (int …Run Code Online (Sandbox Code Playgroud) android ×5
fetch-api ×2
capacitor ×1
devserver ×1
django ×1
expo ×1
laravel-5.2 ×1
localhost ×1
nativescript ×1
networking ×1
node.js ×1
react-native ×1