如何解决在android studio中调用api时无法连接到/192.xxx.x.xxx:8000
当用户单击登录按钮时,我想调用登录 api(django)。我也创建了 apiinterface 和 apiclient。但是当我单击登录按钮时,它向我显示文本:***无法连接到/我的 IP 地址:8000.***我正在使用 pgadmin.Server 已经运行并且 api 在 postman 中工作正常,我不知道为什么它没有连接并给我消息连接失败??请帮帮我!!
public class apiclient {
private static final String BASE_URL="http://192.168.0.102/auth/";
private static apiclient mInstance;
private Retrofit retrofit;
private apiclient(){
retrofit=new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized apiclient getInstance(){
if (mInstance==null){
mInstance=new apiclient();
}
return mInstance;
}
public ApiInterface getApi(){
return retrofit.create(ApiInterface.class);
}
}
Call<ResponseBody> call=apiclient.getInstance().getApi().performUserLogin(username,password);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {
try {
String s =response.body().string();
Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printstacktrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call,Throwable t) {
Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
公共接口 ApiInterface {
@FormUrlEncoded
@POST("registeruser")
Call<ResponseBody>performUserSignIn(@Field("username") String username,@Field("full_name") String full_name,@Field("password") String password,@Field("confirm_password") String confirm_password);
@FormUrlEncoded
@POST("loginuser")
Call<ResponseBody>performUserLogin( @Field("username") String username,@Field("password") String password );
}
这里是 manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<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/Theme.MyApplication"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是日志
2021-06-02 01:30:19.564 30836-30836/? I/art: Late-enabling -Xcheck:jni
2021-06-02 01:30:19.574 30836-30836/? D/TidaProvider: TidaProvider()
2021-06-02 01:30:19.579 30836-30836/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@2571bb
2021-06-02 01:30:19.668 30836-30836/com.example.myapplication W/System: ClassLoader referenced unkNown path: /data/app/com.example.myapplication-1/lib/arm64
2021-06-02 01:30:19.671 30836-30836/com.example.myapplication W/ResourceType: No package identifier when getting name for resource number 0x00000000
2021-06-02 01:30:19.694 30836-30836/com.example.myapplication W/ResourceType: No package identifier when getting name for resource number 0x00000000
2021-06-02 01:30:19.698 30836-30836/com.example.myapplication W/ResourceType: No package identifier when getting name for resource number 0x00000000
2021-06-02 01:30:19.711 30836-30836/com.example.myapplication W/art: Before Android 4.1,method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter,android.content.res.ColorStateList,android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
2021-06-02 01:30:19.717 30836-30836/com.example.myapplication D/AccessibilityManager: current package=com.example.myapplication,accessibility manager mIsFinalEnabled=false,mOptimizeEnabled=true,mIsUiAutomationEnabled=false,mIsInterestedPackage=false
2021-06-02 01:30:19.765 30836-30836/com.example.myapplication I/TypefaceUtils: Typeface has no familyName field
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
2021-06-02 01:30:19.855 30836-30853/com.example.myapplication I/Adreno: QUALCOMM build : 01d2d27,I3d52eaf367
Build Date : 12/10/16
OpenGL ES Shader Compiler Version: XE031.09.00.03
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
2021-06-02 01:30:19.861 30836-30853/com.example.myapplication I/Openglrenderer: Initialized EGL,version 1.4
2021-06-02 01:30:19.861 30836-30853/com.example.myapplication D/Openglrenderer: Swap behavior 1
2021-06-02 01:30:22.752 30836-30836/com.example.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@535c6a8
2021-06-02 01:30:22.913 30836-30836/com.example.myapplication D/NetworkSecurityConfig: No Network Security Config specified,using platform default
2021-06-02 01:30:22.976 30836-30836/com.example.myapplication I/Toast: Show toast from OpPackageName:com.example.myapplication,PackageName:com.example.myapplication
2021-06-02 01:30:31.946 30836-30836/com.example.myapplication W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
解决方法
你的 BASE_URL 是一个本地主机,发布你的 API
,由于它是 HTTP URL 明文流量许可,因此可能会成为一个问题。 如果有效,请尝试以下解决方案。
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。