簡體   English   中英

獲取 Json 響應 API

[英]Getting Json Response Api

我使用 larval 創建了一個身份驗證 API(登錄和注冊),並在 android studio 中對它們進行了測試,它們運行良好,在連接期間我獲得了用戶的所有信息。

I/onSuccess: {"status":"success","data":{"id":2,"name":"user","prenom":"user"...}

我的登錄片段

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

}

我需要從登錄片段中獲取 Id 並將其用於我的個人資料片段,我該怎么做?

我需要在 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();
                        }

                    }

                }

您可以將從身份驗證 API 獲得的響應轉換為 JSONObject,然后獲得所需的值。

例如,

JSONObject authenticationData = new JSONObject(jsonResponse);
try {
   int successId = authenticationData.getInt("id");
catch(JSONException e) {
  e.printStackTrace();
}

然后您可以將 successId 傳遞給您選擇的片段。

閱讀有關JSONObject 的更多信息

如果您的意思是如何在片段之間傳遞數據,那么請向我們展示結構和一些代碼。

我的登錄界面

 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
    );
}

登錄片段


            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();
                            }

                        }

當我登錄時,我在控制台中收到了響應

I/onSuccess: {"status":"success","data":{"id":2,"name":"user","prenom":"user"...}

我的更新配置文件界面

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
    );
}

配置文件片段

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();
                        }

                    }

                }

getID() 重新運行一個錯誤值我需要連接當前用戶的 ID 才能在我的個人資料片段中使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM