How to enter next activity after get Result from SERVER POST?

Viewed 36

im new to android java coding, i need help with my login page, how should i enter to next activity when get result "Status" "Y", tried few solution its not working, what should i do?

btSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            JSONObject jsonObject = new JSONObject();
            
            try {
                
                jsonObject.put("app_id", "123456");
                jsonObject.put("username",etUsername.getText().toString());
                jsonObject.put("password",etPassword.getText().toString());
                jsonObject.put("firebase_token", "123456");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            OkHttpClient client = new OkHttpClient();
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            // put your json here
            RequestBody body = RequestBody.create(JSON, jsonObject.toString());
            Request request = new Request.Builder()
                    .url("url")
                    .post(body)
                    .build();




            Response response = null;
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String myResponse = response.body().string();

                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                TextViewResult.setText(myResponse);
                                if (myResponse == "Y") {
                                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                                    // start the activity connect to the specified class
                                    startActivity(intent);
                                }
                            }
                        });



                    }
                }
            });

        }


    }

    );

Login Page

Picture here, I get lot of result from my server, just need to detect "Status" "Y" and access the next activity, any suggestions?

if (response.isSuccessful()) {
                        String myResponse = response.body().string();
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                 try {
                                     JSONObject obj = new JSONObject(myResponse);
                                     System.out.println(obj.getString("status"));
                                     System.out.println(obj.getString("status_desc"));
                                     System.out.println(obj.getString("token_id"));
                                 }
                                 catch (JSONException e) {
                                     e.printStackTrace();
                                     TextViewResult.setText(JSONObject.getString("status"));
                                     if (JSONObject.getString("status") == "Y") {
                                         Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                                         // start the activity connect to the specified class
                                         startActivity(intent);}
                                  }
                                 }
                        });



                    }

these r coding i added later, but still cant access to next activity, any suggestion?

1 Answers

Your response.body() must be a JsonEntity. something like this

{
key1: "value",
key2: "value"
}

So basically you have to pick the key that is relevant to you using value=response.body.optString("key1") then use the value and check if you're getting the correct value and do the rest of the operations. The response.body() gives you the whole Json.

Related