Java 获取Json响应Api
我使用幼虫创建了一个身份验证API(登录和注册),并在android studio中对其进行了测试,它们运行良好,在连接过程中,我获得了用户的所有信息 I/onSuccess:{“状态”:“成功”,“数据”:{“id”:2,“名称”:“用户”,“prenom”:“用户”…} 我的登录片段Java 获取Json响应Api,java,android,json,android-fragments,Java,Android,Json,Android Fragments,我使用幼虫创建了一个身份验证API(登录和注册),并在android studio中对其进行了测试,它们运行良好,在连接过程中,我获得了用户的所有信息 I/onSuccess:{“状态”:“成功”,“数据”:{“id”:2,“名称”:“用户”,“prenom”:“用户”…} 我的登录片段 Retrofit retrofit = new Retrofit.Builder() .baseUrl(LoginInterface.LOGINURL)
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();
}
}
reformation-reformation=new-reformation.Builder()
.baseUrl(LoginInterface.LOGINURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
LoginInterface api=改装.create(LoginInterface.class);
Call Call=api.getUserLogin(getEmailId,getPassword);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
if(response.body()!=null){
Log.i(“Responsestring”,response.body().toString());
Log.i(“onSuccess”,response.body().toString());
字符串jsonresponse=response.body().toString();
试一试{
JSONObject JSONObject=新的JSONObject(jsonresponse);
if(jsonObject.getString(“status”).equals(“success”)){
Toast.makeText(getActivity(),“登录成功!”,Toast.LENGTH\u SHORT)
.show();
意图;
intent=新的intent(getActivity(),ActivityListEvents.class);
星触觉(意向);
}
}捕获(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
);
}
公共接口登录接口{
字符串LOGINURL=”http://192.168.1.105/AnnocesPFE/public/api/";
@FormUrlEncoded
@发布(“登录”)
调用getUserLogin(
@字段(“电子邮件”)字符串电子邮件,
@字段(“密码”)字符串密码
);
}
我需要从登录片段中获取Id,并将其用于我的profil片段中。我如何才能做到这一点
我需要在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();
}
}
}
reformation-reformation=new-reformation.Builder()
.baseUrl(UpdateProfile.UPDPRO)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
UpdateProfile api=reformation.create(UpdateProfile.class);
Call Call=api.UpdateUsers(getId(),name,prenom,address,email,numtel);
//Log.i(“更新者:”,prenom);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
if(response.body()!=null){
Log.i(“Responsestring”,response.body().toString());
Log.i(“onSuccess”,response.body().toString());
字符串jsonresponse=response.body().toString();
试一试{
JSONObject JSONObject=新的JSONObject(jsonresponse);
if(jsonObject.getString(“status”).equals(“success”)){
Toast.makeText(getActivity(),“更新成功!”,Toast.LENGTH\u SHORT)
.show();
}else if(jsonObject.getString(“status”).equals(“error”)){
/*新建CustomToast()。显示土司(getActivity(),视图,
“”+jsonObject.getString(“消息”)*/
Toast.makeText(getActivity(),“更新失败!”,Toast.LENGTH\u SHORT)
.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
您可以将从身份验证API获得的响应转换为JSONObject,然后获取所需的值
比如说,
JSONObject authenticationData = new JSONObject(jsonResponse);
try {
int successId = authenticationData.getInt("id");
catch(JSONException e) {
e.printStackTrace();
}
然后,您可以将successId传递给您选择的片段
阅读更多关于
如果您的意思是如何在片段之间传递数据,那么请向我们展示结构和一些代码。我的登录界面
public interface LoginInterface {
String LOGINURL = "http://192.168.1.108/AnnocesPFE/public/api/";
@FormUrlEncoded
@POST("login")
Call<String> getUserLogin(
@Field("email") String email,
@Field("password") String password
);
}
public interface UpdateProfile {
String UPDPRO ="http://192.168.1.108/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
);
}
公共接口登录接口{
字符串LOGINURL=”http://192.168.1.108/AnnocesPFE/public/api/";
@FormUrlEncoded
@发布(“登录”)
调用getUserLogin(
@字段(“电子邮件”)字符串电子邮件,
@字段(“密码”)字符串密码
);
}
登录碎片
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();
}
}
改装改装=新改装.Builder()
.baseUrl(LoginInterface.LOGINURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
LoginInterface api=改装.create(LoginInterface.class);
Call Call=api.getUserLogin(getEmailId,getPassword);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
if(response.body()!=null){
Log.i(“Responsestring”,response.body().toString());
Log.i(“onSuccess”,response.body().toString());
字符串jsonresponse=response.body().toString();
试一试{
JSONObject JSONObject=新的JSONObject(jsonresponse);
if(jsonObject.getString(“status”).equals(“success”)){
Toast.makeText(getActivity(),“登录成功!”
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: ",String.valueOf(getId()));
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();
}
}
}