java - 我尝试连接 192.168.1.20/localhost/10.0.2.2 但无法正常工作
问题描述
我试图连接到 Ubuntu 中的改造。它执行失败方法。我也尝试了端口 8080 和 3000。我不知道问题出在哪里。它在服务器 url 上完美运行。
模拟器显示错误无法连接到 10.0.2.2:3000
清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brg_dsk005.pikaainstruction">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
功能:从改造中获取功能
private void TaketestReq() {
/* private JsonObject ApiJsonMap() {
JsonObject gsonObject = new JsonObject();
try {
JSONObject jsonObj_ = new JSONObject();
jsonObj_.put("key", "value");
jsonObj_.put("key", "value");
jsonObj_.put("key", "value");
JsonParser jsonParser = new JsonParser();
gsonObject = (JsonObject) jsonParser.parse(jsonObj_.toString());
//print parameter Log.e("MY gson.JSON: ", "AS PARAMETER " + gsonObject); }
// catch (JSONException e) { e.printStackTrace(); } return gsonObject; }
*/
JsonObject gsonObject = new JsonObject();
JsonObject paramObject = new JsonObject();
JsonObject current_section = new JsonObject();
paramObject.addProperty("test_id", 5);
paramObject.addProperty("user_id", "null");
paramObject.addProperty("org_id", 2);
paramObject.addProperty("schedule_id", 15);
paramObject.addProperty("next_section_id","");
paramObject.addProperty("group_id", "null");
paramObject.addProperty("current_section", String.valueOf(current_section));
JsonParser jsonParser = new JsonParser();
gsonObject = (JsonObject) jsonParser.parse(paramObject.toString());
Log.e("MY gson.JSON: ", "AS PARAMETER " + gsonObject);
Call<Test_Responce> userCall = api_interface.testRes(gsonObject);
userCall.enqueue(new Callback<Test_Responce>() {
@Override
public void onResponse(Call<Test_Responce> call, Response<Test_Responce> response) {
if (response.isSuccessful()){
Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),response.body().getUserName().toLowerCase(),Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(),"else",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Test_Responce> call, Throwable t) {
Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_SHORT).show();
}
});
}
改造连接:这里我附上了改造服务代码
public class RetrofitServiceGenrator {
//private static final String EndPoint = "http://pikatestengine.brigita.co/testengine/public/api/";
private static final String EndPoint = "http://10.0.2.2:3000/testengine/public/api/";
private static Api_interface retrofitClient = null;
public static Api_interface getRetrofitClient(Context context){
if (retrofitClient == null){
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjAyODkyMzI5NzdjNjczZTlmZDExYmVkOGYzNTBiYTY2YjhmMjNjNWVkYjU4ZDM5NmMxM2I3YjJhYjZhOWVkYmMwZTlkZWI0MDhiYmI3MjAzIn0.eyJhdWQiOiIzIiwianRpIjoiMDI4OTIzMjk3N2M2NzNlOWZkMTFiZWQ4ZjM1MGJhNjZiOGYyM2M1ZWRiNThkMzk2YzEzYjdiMmFiNmE5ZWRiYzBlOWRlYjQwOGJiYjcyMDMiLCJpYXQiOjE1MzEyMTk3NjksIm5iZiI6MTUzMTIxOTc2OSwiZXhwIjoxNTYyNzU1NzY5LCJzdWIiOiIyNCIsInNjb3BlcyI6W119.dA6OvGg4rwU13bzVITzOcJpdhUJico1YwaJpAwvcxISghk7yVqEhJunDByAOR1nk5bScwUv5RUERRIbXd1QPk5mxkbmZdZNPGRh__QswWLLaorGB0gma7NwmS42V3zKH0e-n9T6ZJivZP2PgmuTwnTouFv3dE3uf-jc0dK9dYZMw9ClzEY-U7_W75bwUDKOROxFfQf0yIuOR4zPMD0LePgWYheKRI1soYoEfT1hfAjUEMFpjGRlyQy_aVRXYVQJunLyeuaHEYzGhXeNu_Yy1-tiBUqaHDSH6zIYfYXUKtD4-RH33iZcOIEuIh9QPzgf-v-WEPorlroMed9HlLv-ctmcptj9p4kIW8g5jZ6NEuReOKGcjiTFqrikPhiYw7SEhU_Q_lo0gUentJSO_VjQUPsOO0WVAKOGRpgnuDIjs3H2MZTTiae_6cbcLJUGc8_irSTY9i_4AewBQ2e3EQMFdUVf8fFYvYVP3OEA-Ys1bNZiGQCckhLbLroQDgDF6NbnVpUdqvWMMnKignXAtxKAiNcsdRHbfw6DH-Trg5syd0152yXVGxqCx9SpW7d0ypOh98lwwDGr10B7BduRLzkJDrTREP6TwHf34lJ4FAN1LdDvUAjwoaP-d8gmJw6sAl15_in25G6emcMbwXeW0EC-sMY0A6qP5NzKYgYeBsLk8Z9k")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EndPoint)
.addConverterFactory(GsonConverterFactory.create())
.client(client).build();
retrofitClient = retrofit.create(Api_interface.class);
}
return retrofitClient;
}
}
界面:
@POST("take_tests")
Call<Test_Responce> testRes (@Body JsonObject paramObject);
解决方案
可能是,它无法访问 localhost 网络,请在 android manifest 中添加以下权限并尝试
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
推荐阅读
- pandas - Knn 赋予距离上的特定特征更多的权重
- angular - Angular 7 从 web api 返回一个 json 对象并在页面上访问
- java - 在 Java 中将编码为 UTF-8 的字符串转换为 TIS620(泰语编码)。有哪些方法可以做到这一点,是否有任何数据丢失?
- xml - 从 Microsoft.AspNetCore.OData 7 而不是 JSON 返回 XML,并且 Produces 设置为 XML 返回 406
- ionic-framework - 找不到任何模块 - Ionic 4
- linux - 从路径中提取最后一个斜杠的文件名
- php - htaccess .php 和 .jpeg 之间的冲突
- angular - 如何使用 Angular 8 应用程序和 Apache2 配置 Rendora 服务器端渲染
- javascript - Opening maxmind DB and accessing it in nodejs
- docker - Kubectl logs 命令无法打印输出