在Android移动应用中使用带有用户名和密码的URL登录 [英] Login using url with username and password in android mobile app
本文介绍了在Android移动应用中使用带有用户名和密码的URL登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用网站的android移动应用程序,我具有url用户名和密码.我需要通过app登录.应该怎么办?我需要使用任何库进行身份验证吗,我们将不胜感激.感谢您!
I am working on android mobile application for website I have a url username and password.I need to login through app.How it should be done? Do I need to use any library for authentication.Any help would be appreciated.Thanks in advance!
推荐答案
首先,您必须实现一些rest api库.
First you have to implement some rest api libraries.
implementation('com.squareup.retrofit2:retrofit:2.1.0') {
exclude module: 'okhttp'
}
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.0'
implementation 'com.squareup.okhttp3:okhttp:4.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
然后在创建该类的ApiClient ..之后,在其中编写以下代码
Then after create class ApiClient.. in this class write below code in it
public class ApiClient {
private static Retrofit retrofit = null;
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}}
现在创建接口ApiInterface并在其中编写以下代码.
Now create Interface ApiInterface and write below code in it..
public interface ApiInterface {
@FormUrlEncoded
@POST("login/")
Call<Your Pojo Class> loginuser(@Field("method") String method, @Field("login_name")
String loginname,@Field("password") String password);}
在您的MainActivity中编写以下代码.
In your MainActivity Write belowe code..
APIInterface apiInterface;
apiInterface = APIClient.getClient().create(APIInterface.class);
Call<EarnAmount> call = apiInterface.loginuser("loginuser", loginid,password);
call.enqueue(new Callback<Your Pojo>() {
@Override
public void onResponse(Call<Your Pojo> call, Response<Your Pojo> response) {
//you will get some response
//you can handle response from here
}
@Override
public void onFailure(Call<Your Pojo> call, Throwable t) {
//check internet connection or something went wrong
}
}); }
这篇关于在Android移动应用中使用带有用户名和密码的URL登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文