(ANDROID) How to POST JSON to server by enter our username and password (Login Page)?

Viewed 22

i need help from how to input our username and password and post them to my server. i try few way its no working, tried .getText() and .toString() but still working. tried few solution from other tutorial web get nothing too. Please help. Thanks

public class MainActivity extends AppCompatActivity {


String etUsername;
String etPassword;
Button btSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etUsername = ((EditText)findViewById(R.id.et_username)).getText().toString();
    etPassword = ((EditText)findViewById(R.id.et_password)).getText().toString();
    btSubmit = findViewById(R.id.bt_submit);

    TextView TextViewResult;
    TextViewResult = findViewById(R.id.text_view_result);

    OkHttpClient client = new OkHttpClient();

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

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this
                );

i add here

            JSONObject jsonObject = new JSONObject();
            // Json here
            try {
             
                jsonObject.put("app_id", "123");
                jsonObject.put("username",etUsername);
                jsonObject.put("password",etPassword);
                jsonObject.put("firebase_token", "123");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            OkHttpClient client = new OkHttpClient();
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(JSON, jsonObject.toString());
            Request request = new Request.Builder()
                    .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);
                            }
                        });


                    }
                }
            });
        }

    }

    );


    }

    }

no error for my coding after running it, just i cant get my correct data POST to server. any suggestion?

1 Answers

Done

EditText etUsername,etPassword;

etUsername = (EditText) findViewById(R.id.et_username);
etPassword = (EditText) findViewById(R.id.et_password);


 try {
                
                jsonObject.put("app_id", "Cu7HAc8Udro3rePhIPoBrubLBr7hljIcopICu8uGlwlSwAprOprlYOd9aXAchugu");
                jsonObject.put("username",etUsername.getText().toString());
                jsonObject.put("password",etPassword.getText().toString());
                jsonObject.put("firebase_token", "abc123");
            } catch (JSONException e) {
                e.printStackTrace();
            }
Related