Retrofit Rest API无法在Android中使用

时间:2019-06-06 10:20:11

标签: android rest web-services retrofit2

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

遇到错误

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

清单

<?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>

MainActivity

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接口

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响应和EndPoint

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

LoginResponse 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)

2 个答案:

答案 0 :(得分:1)

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

尝试在manifest.xml中使用这一行代码

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

答案 1 :(得分:1)

确保您具有权限:

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

在浏览器或Postman上检查您的URL, 如果可行,请尝试卸载您的应用并再次安装。

PS:不要只单击“运行应用程序”以进行卸载和安装,请转到模拟器并从那里进行卸载。 enter image description here

enter image description here