gpt4 book ai didi

android - Retrofit Rest API 无法在 Android 中使用

转载 作者:行者123 更新时间:2023-11-29 23:06:21 25 4
gpt4 key购买 nike

我面临与在 android 中使用 Rest Api 相关的问题。我正在使用 Retrofit APi 来使用该服务。它无法使用其余 API。

获取错误

java.net.SocketException: socket failed: EPERM (Operation not permitted)

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nagata.billing">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission android:name="android.permission.INTERNET"></permission>
<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"
android:usesCleartextTraffic="true">
<activity
android:name=".Home"
android:label="@string/title_activity_home"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

主要 Activity

Call<LoginResponse> call= RetrofitClient.getInstance().getApi().userLogin(client,userName,password);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
LoginResponse loginResponse=response.body();
if(loginResponse.getStatus() ==200){
Intent intent = new Intent(MainActivity.this, Home.class);
startActivity(intent);
finish();
Toast.makeText(MainActivity.this,loginResponse.getMessage(),Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,loginResponse.getMessage(),Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
// Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_LONG).show();
}
});

RetrofitClient 客户端

import com.nagata.billing.api.Api;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

private static final String BASE_URL="http://192.168.1.22:8080/NagataInternalServer/";

private static RetrofitClient mInstance;
private Retrofit retrofit;

private RetrofitClient(){
retrofit=new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}

public static synchronized RetrofitClient getInstance(){
if(mInstance == null){
mInstance=new RetrofitClient();
}
return mInstance;
}

public Api getApi(){
return retrofit.create(Api.class);
}
}

API接口(interface)

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface Api {

@FormUrlEncoded
@POST("token/android-generate-token")
Call<LoginResponse> userLogin(@Field("client") String client,@Field("username") String username,
@Field("password") String password
);

}

build.gradel

 implementation 'com.android.volley:volley:1.1.0'
implementation 'com.google.code.gson:gson:2.4'

我的 JSON 响应和端点

 String URL = "http://192.168.1.22:8080/NagataInternalServer/token/android-generate-token";

登录响应 DTO

public class LoginResponse {
private int status;
private String message;
private AuthToken result;

public LoginResponse(int status, String message, AuthToken result) {
this.status = status;
this.message = message;
this.result = result;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Object getResult() {
return result;
}

public void setResult(AuthToken result) {
this.result = result;
}

}

{
"status": 200,
"message": "success",
"result": {
"token": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJLdWxkZWVwIiwic2NvcGVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0FETUlOIn1dLCJpc3MiOiJodHRwOi8vZGV2Z2xhbi5jb20iLCJpYXQiOjE1NTk4MDA3NDQsImV4cCI6MTU1OTgxODc0NH0.wiVd3qu1CX8JxZ4ncy4AcetCXCYrGgiYA5K7CKre_Ho",
"username": "Kuldeep"
}
}

错误日志:

java.net.SocketException: socket failed: EPERM (Operation not permitted)

最佳答案

确保你有权限:

<uses-permission android:name="android.permission.INTERNET"/>

在浏览器上或使用 Postman 检查您的 URL,如果有效,请尝试卸载您的应用并重新安装。

PS:不要只点击运行应用程序来卸载和安装,转到模拟器并从那里卸载它。 enter image description here

enter image description here

关于android - Retrofit Rest API 无法在 Android 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56475563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com