[英]Why no http requests are sent? Android Studio - Retrofit
I'm using Retrofit in Android studio - java.我在 Android Studio 中使用 Retrofit - java。 I'm trying to send an http request to a specific ip and when i try to sniff the packets while running the application in the emulator, i can't see any of the http packets on the list which sent to the specific ip.
我正在尝试向特定 ip 发送 http 请求,当我在模拟器中运行应用程序时尝试嗅探数据包时,我看不到列表中发送到特定 ip 的任何 http 数据包。
This is where I try to send the http requests:这是我尝试发送 http 请求的地方:
IService service = retrofit.create(IService.class);
Call<LoginResponse> call = service.Login(loginRequest);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response)
{
Response = response.body();
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Response = new LoginResponse("Error with the communication with the server!", null);
}
});
return (LoginResponse) Response;
This is my service:这是我的服务:
public interface IService
{
@POST("api/users/login")
Call<LoginResponse> Login(@Body LoginRequest loginRequest);
}
Can't understand where I did wrong.无法理解我哪里做错了。 Thanks.
谢谢。
In Android View Structure :在 Android 视图结构中:
Manifest -> AndroidManifest.xml :清单-> AndroidManifest.xml :
Also provide usesCleartextTraffic so it won't give you error to send resposne to ip还提供 usesCleartextTraffic 所以它不会给你发送 resposne 到 ip 的错误
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
Please check your URL, if it's http or https.请检查您的 URL,如果它是 http 或 https。 If your URL is http and you got this error,
如果您的 URL 是 http 并且出现此错误,
CLEARTEXT communication to ** not permitted by network security policy
网络安全策略不允许与 ** 的 CLEARTEXT 通信
it's related that cleartext support is disabled by default, starting with Android 9 (API level 28).与默认情况下禁用明文支持有关,从 Android 9(API 级别 28)开始。
https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted
In this case, please add this android:usesCleartextTraffic="true" in the application tag, of the AndroidManifest.xml.在这种情况下,请在 AndroidManifest.xml 的应用程序标签中添加这个 android:usesCleartextTraffic="true"。
<application
android:name=".App"
android:allowBackup="false"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
If not, please post your error description.如果没有,请发布您的错误描述。 Hope to be helpful.
希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.