我试图在Ubuntu中连接到改造。它执行失败方法。我也尝试过8080和3000端口。我不知道问题出在哪里。它完美地在服务器URL上工作。
模拟器显示错误为 无法连接到10.0.2.2:3000
manifest.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);
答案 0 :(得分:0)
可能是可能的,它无法访问localhost网络,请在android清单中添加以下权限,然后尝试
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
答案 1 :(得分:0)
我希望这段代码对您有用, 试试这个。
10.0.2.2 is your local ip? print the url in log and share, can you post your error log