我使用幼虫创建了一个身份验证API(登录和注册),并在android studio中对其进行了测试,它们现在运行良好,在连接期间,我可以获取用户的所有信息。
I / onSuccess:{“状态”:“成功”,“数据”:{“ id”:2,“名称”:“用户”,“ prenom”:“用户” ...}
我需要从登录片段中获取连接的用户ID,并将其用于我的个人资料片段中。我该怎么办?
我的登录片段
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(LoginInterface.LOGINURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
LoginInterface api = retrofit.create(LoginInterface.class);
Call<String> call = api.getUserLogin(getEmailId, getPassword);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("Responsestring", response.body().toString());
Log.i("onSuccess", response.body().toString());
String jsonresponse = response.body().toString();
try {
JSONObject jsonObject = new JSONObject(jsonresponse);
if (jsonObject.getString("status").equals("success")) {
Toast.makeText(getActivity(), "Login Successfully!", Toast.LENGTH_SHORT)
.show();
Intent intent;
intent = new Intent(getActivity(), ActivityListEvents.class);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
我的登录界面
public interface LoginInterface {
String LOGINURL = "http://192.168.1.105/AnnocesPFE/public/api/";
@FormUrlEncoded
@POST("login")
Call<String> getUserLogin(
@Field("email") String email,
@Field("password") String password
);
}
我需要在ProfileFragment中使用它
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(UpdateProfile.UPDPRO)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
UpdateProfile api = retrofit.create(UpdateProfile.class);
Call<String> call = api.UpdateUsers(getId(),name,prenom,adresse,email,numtel);
//Log.i("updateUsers: ",prenom);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("Responsestring", response.body().toString());
Log.i("onSuccess", response.body().toString());
String jsonresponse = response.body().toString();
try {
JSONObject jsonObject = new JSONObject(jsonresponse);
if (jsonObject.getString("status").equals("success")) {
Toast.makeText(getActivity(), "Update Successfully!", Toast.LENGTH_SHORT)
.show();
} else if (jsonObject.getString("status").equals("error")) {
/* new CustomToast().Show_Toast(getActivity(), view,
" " + jsonObject.getString("message"));*/
Toast.makeText(getActivity(), "Update failed!", Toast.LENGTH_SHORT)
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我的个人资料界面
public interface UpdateProfile {
String UPDPRO ="http://192.168.1.105/AnnocesPFE/public/api/";
@FormUrlEncoded
@PUT("users/{id}")
Call<String> UpdateUsers(
@Path("id") int id,
@Field("name") String name,
@Field("prenom") String prenom,
@Field("adresse") String adresse,
@Field("email") String email,
@Field("numtel") String numtel
);
}