As mentioned I get the above error which I know is because my application is doing networking in UI thread. I went through a lot of stackoverflow questions which advise to use AsyncTask for this purpose. From what I understand asynctask is asynchronous and will run independently in the background. But I need to fetch the data from http and display on the main thread. So basically my UI thread should be blocked till I have the JSON fetched so that I can display it.
My questions are 1) Since I need to run http networking in another thread how do I do it? 2) Do I use an async thread? 3) How do I block my UI thread for the async thread to fetch the result? 4) How do I pass the result from async thread back to UI thread?
This is the current JSON parser class that I use.
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONfromURL(String url) {
    // initialize
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;
    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
        return null;
    }
    // convert response to string
    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();
        result = sb.toString();
        Log.e("log_tag", "JSON data" + result);
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
        return null;
    }
    // try parse the string to a JSON object
    try {           
        jArray = new JSONArray(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
        return null;
    }
    return jArray;
}
}
And this is my MainActivity where I call JSONparser to fetch some data that I need to display
    JSONArray json = jParser.getJSONfromURL(temp);
    if (json == null) {
        return -1;
    }
    for (int i = 0; i < json.length(); i++) {
        try {
            JSONObject c = json.getJSONObject(i);
            // Getting Array of Contacts
            // Storing each json item in variable
            asr_iq = c.getString("lMAsr");
            sunrise_iq = c.getString("lMSunrise");
            fajr_iq = c.getString("lMFajr");
            isha_iq = c.getString("lMIsha");
            dhuhr_iq = c.getString("lMDhuhr");
            maghrib_iq = c.getString("lMMaghrib");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
 
     
    