Fix android.os.NetworkOnMainThreadException in android API Call

In Android when you want some API call and you are creating a HTTP request then you always get NetworkOnMainThreadException. This exception is raised due to request is created on main thread, Main thread is used for Application internal processing.

To call an API you need to create second thread so that main thread could not affect. To create second thread in android you can use AsyncTask.

Create an Interface:

public interface HTTPCallback {
    void processFinish(String output);
    void processFailed(int responseCode, String output);
}

Then create  class:

public class HTTPRequest extends AsyncTask<String, Void, String>{
    public String requestURL="";
    public HashMap<String, String> postDataParams;
    public HTTPCallback delegate = null;//Call back interface
    public int res_code=0;

    public HTTPRequest(String requestURL, HashMap<String, String> postDataParams,HTTPCallback asyncResponse){
        this.delegate = asyncResponse;//Assigning call back interfacethrough constructor
        this.postDataParams=postDataParams;
        this.requestURL=requestURL;
    }
    @Override
    protected String doInBackground(String... params) {
        return performPostCall(requestURL,postDataParams);
    } 

    @Override
    protected void onPostExecute(String result) {
        //super.onPostExecute(result);
        if(res_code==HttpURLConnection.HTTP_OK){
            delegate.processFinish(result);
        }else{
            delegate.processFailed(res_code, result);
        }
    }

    public String  performPostCall(String requestURL,
                                   HashMap<String, String> postDataParams) {
        Log.e("HTTP Request URL",requestURL);
        URL url;
        String response = "";
        try {
            url = new URL(requestURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(30000);
            conn.setConnectTimeout(60000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));
            writer.flush();
            writer.close();
            os.close();
            int responseCode=conn.getResponseCode();
            Log.e("HTTP Response Code", Integer.toString(responseCode));
            res_code=responseCode;
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            }else if (responseCode == 500) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            }
            else {
                response="";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(Map.Entry<String, String> entry : params.entrySet()){
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            Log.e("POST KEY VAL",entry.getKey()+","+entry.getValue());
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }
        Log.e("Request",result.toString());
        return result.toString();
    }
}

Now you can call API easily:

HashMap<String, String> req_data = new HashMap<String, String>();;
req_data.put("username","Sheetal");
req_data.put("password","123456");
HTTPRequest requestHttp = new HTTPRequest("http://example.com/xyz/login", req_data, new HTTPCallback() {
    @Override
    public void processFinish(String response) {
          Log.e("Response",response);
    }

    @Override
    public void processFailed(int responseCode, String output) {
          Log.e("Response Failed", Integer.toString(responseCode) + " - " + output);
    }
});
requestHttp.execute();

 

Alternatively you can use Volley or Retrofit Library to make an API Call in android.

Keywords: