0

I am new to android and have wasted hours in this issue, decided to post after referring similar posts. I am implementing a android app login from API which is working but i am getting android.os.networkonmainthreadexception, so trying to put uri request in background task, Here is reference tutorial

LoginActivity.java

// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {    
        String email = inputEmail.getText().toString();
        String password = inputPassword.getText().toString();
        UserFunctions userFunction = new UserFunctions();
        Log.d("Button", "Login");
        JSONObject json = userFunction.loginUser(email, password);
        String check = "";
        try {
            check = json.getString(KEY_TOKEN).toString();
        } catch (JSONException e1) {
            check = null;
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // check for login response
        try {
            System.out.println("a");
            if (check != null) {
                System.out.println("aa");
                loginErrorMsg.setText(""); 
                    // user successfully logged in
                    // Store user details in SQLite Database
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());


                    // Clear all previous data in database
                    userFunction.logoutUser(getApplicationContext());
                    //db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                      
                    db.addUser(json.getString(KEY_NAME), json.getString(KEY_EMAIL), json.getString(KEY_UID), "created_at", json.getString(KEY_TOKEN), json.getString(KEY_LOGINABLE_TYPE), json.getString(KEY_SCHOOL_ID));
                    // Launch Dashboard Screen
                    Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                    // Close all views before launching Dashboard
                    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(dashboard);

                    // Close Login Screen
                    finish(); 
            }
            else{
                System.out.println("b");
                // Error in login
                loginErrorMsg.setText(json.getString(KEY_NOTICE));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
});

UserFunctions.java

................
public class UserFunctions {

    private JSONParser jsonParser;

    private static String loginURL = "http://10.0.2.2:3000/users/sign_in";
    private static String registerURL = "http://10.0.2.2:3000/users/";

    private static String login_tag = "login";
    private static String register_tag = "register";

    // constructor
    public UserFunctions(){
        jsonParser = new JSONParser();
    }

    /**
     * function make Login Request
     * @param email
     * @param password
     * */
    public JSONObject loginUser(String email, String password){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("user[email]", email));
        params.add(new BasicNameValuePair("user[password]", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
    ...........
 }

JSONParser.java

package com.example.androidhive.library;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);            
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}
inox
  • 131
  • 1
  • 2
  • 12

2 Answers2

1

That Android.Os.NetworkOnMainThreadException you're getting is thrown because you're trying to do network-related operation (in this case, calling your API) on the main thread.

However, you're right on path when you mentioned AsyncTask as it was designed for this particular use-case. It's fairly simple to use, too. Check this out:

private class LoginValidatorTask extends AsyncTask<String, Void, Integer> {

    private static final int RESULT_OK = 0;
    private static final int RESULT_FAILED = 1;

    private ProgressDialog loadingDialog;

    @Override
    protected void onPreExecute() {

        // Create a ProgressDialog to let user know of the ongoing operation..
        loadingDialog = new ProgressDialog(YourActivity.this);
        loadingDialog.setMessage("Logging in..");
        loadingDialog.setIndeterminate(true);
        loadingDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {

        // Do your main login logic here.
        // Using RETURN_OK as a return value if the login succeeded
        // ...or RETURN_FAILED otherwise.
    }

    @Override
    protected void onPostExecute(Integer resultCode) {

        switch (resultCode) {
            case SUCCESS:
                // Dismiss the ProgressDialog.
                loadingDialog.dismiss();

                // Do something here if login succeeded.
                break;
            case FAILED:
                // Dismiss the ProgressDialog.
                loadingDialog.dismiss();

                // Do something here if login failed.
                break;
            default:
                // Do nothing..
        }
    }
}

There, you have yourself a working AsyncTask for logging in into your app. To actually use it though, you'd have to do something like this:

    new LoginValidatorTask().execute(username, password);

Do note that those values passed (username and password) will be available in your AsyncTask as a String[] in its doInBackground(String... params) method.

Hope this helped!

Hadi Satrio
  • 4,272
  • 2
  • 24
  • 45
0

Check this...

Example Async Task that send a request to url...

Bouncing between "Adapter is detached" and "No wrapped connection" with HttpClient

Community
  • 1
  • 1
Amitabha Biswas
  • 3,281
  • 1
  • 11
  • 23