Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends
- AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final EditText id = (EditText) findViewById(R.id.loginid);
- final EditText pass = (EditText) findViewById(R.id.loginpass);
- Button b1 = (Button) findViewById(R.id.buttonlogin);
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String username = id.getText().toString();
- String password = pass.getText().toString();
- if (isNetworkAvailable()) {
- RequestBody formBody = new FormBody.Builder()
- .add("username", username)
- .add("password", password)
- .build();
- try {
- post(ConstantValues.BASE_URL, formBody, new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- Log.e("JSONDemo", "IOException", e);
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- String res = response.body().string();
- Log.e("res", " " + res);
- try {
- JSONArray jsonarr = new JSONArray(res);
- for (int i = 0; i < jsonarr.length(); i++) {
- JSONObject jsonobj = jsonarr.getJSONObject(i);
- final String namee = jsonobj.getString("FIRST_NAME");
- Log.e("name", " " + namee);
- }
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- // you can access all the UI componenet
- }
- });
- } catch (Exception e) {
- Log.e("JSONDemo", "onResponse", e);
- }
- }
- });
- } catch (Exception e) {
- Log.e("JSONDemo", "Post Exception", e);
- }
- }
- }
- });
- }
- private boolean isNetworkAvailable() {
- ConnectivityManager connectivityManager
- = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
- return activeNetworkInfo != null && activeNetworkInfo.isConnected();
- }
- private final OkHttpClient client = new OkHttpClient();
- Call post(String url, RequestBody formBody, Callback callback) throws IOException {
- Request request = new Request.Builder()
- .url(url)
- .post(formBody)
- .build();
- Call call = client.newCall(request);
- call.enqueue(callback);
- return call;
- }
- public void showAlertDialog(String title, String message) {
- final AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
- builder1.setTitle(title);
- builder1.setMessage(message);
- builder1.setCancelable(true);
- builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.cancel();
- }
- });
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- builder1.show();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement