Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复Kotlin Android应用程序中的API身份验证问题?_Java_Android_Api_Authentication_Kotlin - Fatal编程技术网

Java 如何修复Kotlin Android应用程序中的API身份验证问题?

Java 如何修复Kotlin Android应用程序中的API身份验证问题?,java,android,api,authentication,kotlin,Java,Android,Api,Authentication,Kotlin,我在Laravel中开发了一个API,用于通过身份验证读取一些数据。现在在我的应用程序中,我需要在我的头中发送令牌,以便API响应。每次我使用应用程序登录时,我都会毫无问题地获得令牌,但我无法在头中提供令牌,它会返回一个空令牌响应。现在,每次我重新打开我的应用程序,一切都很好,但问题只出现在我登录时 请阅读我的代码,并说如果你发现了错误 这是我的(AppServer.kt)。我使用它将令牌添加到我的头中 class AppServer private constructor(privat

我在Laravel中开发了一个API,用于通过身份验证读取一些数据。现在在我的应用程序中,我需要在我的头中发送令牌,以便API响应。每次我使用应用程序登录时,我都会毫无问题地获得令牌,但我无法在头中提供令牌,它会返回一个空令牌响应。现在,每次我重新打开我的应用程序,一切都很好,但问题只出现在我登录时

请阅读我的代码,并说如果你发现了错误

这是我的(AppServer.kt)。我使用它将令牌添加到我的头中

    class AppServer private constructor(private val client: INetworkClient) : IAppServer {

      private val gson = Gson()

      companion object {
        private var instance: AppServer? = null
        fun getInstance(): AppServer {
          val token = AppLoggedInUser.getInstance()?.userProfile?.userToken ?: ""

          if (instance == null)
            instance = AppServer(
              MixedNetworkClient(
                mHeaders = mutableMapOf(
                  Headers.CONTENT_TYPE to "application/json",
                  "X-Requested-With" to "XMLHttpRequest",
                  "Authorization" to "Bearer $token"
                ),
                mBasePath = "https://myWebsiteURL.com/"

              )
            )
          return instance!!
        }
      }

      override fun getPurchasedItems(): Observable<Pair<ResponseState, List<OrderInfo>?>> {
        return Observable.create<Pair<ResponseState, List<OrderInfo>?>> { emitter ->

          val (items, statusCode, msg, error) =
            client.get(
              "api/users/${AppLoggedInUser.getInstance().userProfile.userId}/orders",
              { responseStr ->
                try {

                  val orders = mutableListOf<OrderInfo>()
                  val jArray = JSONArray(responseStr)
                  for (i in 0 until jArray.length()) {
                    val jObject = jArray.getJSONObject(i)
                    val order =
                      gson.fromJson<OrderInfo>(jObject.toString(), OrderInfo::class.java)

                    if (order.isPaymentSuccessful)
                      orders.add(order)
                  }
                  if (orders.isEmpty())
                    emitter.onNext(pair(empty(), null))
                  else
                    emitter.onNext(pair(success(), orders))

                } catch (t: Throwable) {
                  Timber.e(t)
                  emitter.onNext(pair(ResponseState.internalError(t), null))
                }
              })
        }.attachSchedulers()
      }

      override fun getDiscountDetailsById(id: Int): Observable<Pair<ResponseState, DiscountDetails?>> {
        return Observable.create<Pair<ResponseState, DiscountDetails?>> { emitter ->
          try {
            client.post("api/post", listOf("post_id" to id.toString())) { responseStr ->
              val jObject = JSONObject(responseStr)
              var discount: DiscountDetails? = null
              if (jObject.has("ID"))
                discount =
                  gson.fromJson<DiscountDetails>(jObject.toString(), DiscountDetails::class.java)
              if (discount != null)
                emitter.onNext(pair(success(), discount))
              else
                emitter.onNext(pair(notFound404(), null))
            }
          } catch (t: Throwable) {
            Timber.e(t)
            emitter.onNext(pair(ResponseState.internalError(t), null))
          }
        }.attachSchedulers()
      }
    }

我想问题出在我的第二节课上。

@halfer啊,谢谢你提到“Laravel”。我会尽力的@阿特拉斯·皮奥:没问题。这与任何软件、库或语言的名称有关。
package com.fartaak.gilantakhfif.utilities;

import android.content.Context;
import com.fartaak.gilantakhfif.backend.server.ParsingGSON;
import com.fartaak.gilantakhfif.model.UserProfile;

public class AppLoggedInUser extends AppPreferences {

  public static final String NAM_LOGIN_SdPs = "login";

  public static final String KEY_USER_TOKEN = "tokenPor";

  private static AppLoggedInUser mInstance;

  private final String KEY_LOGIN = "prefP";

  private boolean isCompletelyRegistered;


  public static void init(Context context) {
    if (mInstance == null) {
      mInstance = new AppLoggedInUser(context);
    }
  }

  public static AppLoggedInUser getInstance() {
    if (mInstance != null) {
      return mInstance;
    } else {
      throw new IllegalStateException(
          "you should call init to initialize only once per app launch before calling getInstance");
    }
  }

  private AppLoggedInUser(Context context) {
    super(NAM_LOGIN_SdPs, context);
  }

    public String getUserToken() {
        return getField(KEY_USER_TOKEN);
    }


  public void clearUserProfile() {
    removeField(KEY_LOGIN);
  }

  public UserProfile getUserProfile() {
    String data = getField(KEY_LOGIN);
    if (data == null) {
      return null;
    }
    //return new Gson().fromJson(data, UserProfile.class);
    return ParsingGSON.getInstance().getParsingJSONObject(data, UserProfile.class, null);
  }

  public boolean isRegisterCompleted() {
    if (!isCompletelyRegistered) {
      UserProfile userProfile = getUserProfile();
      if (userProfile.getUserName() == null) {
        return false;
      } else {
        isCompletelyRegistered = true;
      }
    }
    return true;
  }

  public boolean isUserProfile() {
    return isField(KEY_LOGIN);
  }

  public void setUserProfile(UserProfile userProfile) {
    String json = ParsingGSON.getInstance().toJSONObject(userProfile, UserProfile.class, null);
    setField(KEY_LOGIN, json);
  }
}