0

when I sent post login data use login button like this :

loginbutton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final String user = email.getText().toString().trim();
            final String pwd = password.getText().toString().trim();

            if (email.getText().toString().equals("")) {
                Utils.toast(context, "Username empty...");
            } else if (password.getText().toString().equals("")) {
                Utils.toast(context, "Password empty...");
            } else if (!isValidEmail(user)) {
                email.setError("Invalid Email");
            } else if (user.length() < 2) {
                Utils.toast(context, "Username to short...");
            } else if (pwd.length() < 2) {
                Utils.toast(context, "Password to short...");
            } else if (!isValidPassword(pwd)) {
                password.setError("Invalid Password");
            } else {
                progress.setVisibility(View.VISIBLE);
                SendfeedbackJob job = new SendfeedbackJob();
                job.execute(user, pwd);
            }
        }
    });

private class SendfeedbackJob extends AsyncTask<String, Void, String> {
    private static final String LOG_TAG = "UserLoginTask";
    @Override
    protected String doInBackground(String... params) {
        String user = params[0];
        String pwd = params[1];
        // do above Server call here
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("email", user ));
        postParameters.add(new BasicNameValuePair("password", pwd ));

        String responseString = null;

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.0.219:90/auth/login");

            // no idea what this does :)
            httppost.setEntity(new UrlEncodedFormEntity(postParameters));

            // This is the line that send the request
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();
            String responseAsText = EntityUtils.toString(response.getEntity());
            Utils.log("daftar isi: " + responseAsText);
            JSONObject loginjson = new JSONObject(responseAsText);
            Utils.log("json object: " + loginjson);
            String roleString = loginjson.getString("role");
            Utils.log("role: " + roleString);
            if(roleString.equals("member")){
                Intent intent = new Intent(context, home.class);
                startActivity(intent);
            }else if(roleString.equals("studio")){
                Intent intent = new Intent(context, VendorDashboard.class);
                startActivity(intent);
            }
        }
        catch (Exception e)
        {
            Log.e(LOG_TAG, String.format("Error during login: %s", e.getMessage()));
        }
        return "processing";
    }

    @Override
    protected void onPostExecute(String message) {
        //process message
    }
}

actually it will get feedback JSON response like this :

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxNCIsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMC4yMTk6OTBcL2F1dGhcL2xvZ2luIiwiaWF0IjoiMTQ0NTI0NzIyMSIsImV4cCI6IjE0NDY0NTY4MjEiLCJuYmYiOiIxNDQ1MjQ3MjIxIiwianRpIjoiNTY4MDEyZDUwNTg1NDFjM2UzNGVjMGViYzMzZDkzMGQifQ.zw5c5kLIlvPYIMhEzEnF_fCOu77XTq2prcDtSHJY7bk","role":"studio"}

How to get role (JSON response feedback) so I can use if and else if statement after execute HTTP Post Request (after Utils.toast(context, responseAsText);)?

Update after I used asynctask, it did not produce error onclick. Update it is done. Thanks mr.shreyash.

Question closed.

Tanya Kode
  • 57
  • 7
  • You can't run long running task on UI Thread. You needed to use thread or async task.....Duplicate of...[link](http://stackoverflow.com/questions/9413625/android-android-os-networkonmainthreadexception) – shreyash mashru Oct 26 '15 at 03:07
  • Please describe the correct async task, I did not understand. like this? private class SendfeedbackJob extends AsyncTask protected String doInBackground(String user, String pwd) – Tanya Kode Oct 26 '15 at 03:34
  • I called onclick like this: SendfeedbackJob job = new SendfeedbackJob(); job.execute(user, pwd); – Tanya Kode Oct 26 '15 at 03:37
  • Please refer how to call async task. Because there are many way you can define.[AsyncTask Doc](http://developer.android.com/reference/android/os/AsyncTask.html) also [an example](http://stackoverflow.com/questions/9671546/asynctask-android-example/9671602#9671602) – shreyash mashru Oct 26 '15 at 03:50
  • I update the code. Like that, mr.shreyash? But I still cannot get that json object role. – Tanya Kode Oct 26 '15 at 04:26
  • Can you please update the question with proper code and log. So all can look into it and can try to give you solution. – shreyash mashru Oct 26 '15 at 04:28
  • Ok good you are good to go. Just return that string and you will get that string in onPostExecute method. – shreyash mashru Oct 26 '15 at 04:43

0 Answers0