Note that there are some explanatory texts on larger screens.

plurals
  1. POimplementing AsyncTask
    primarykey
    data
    text
    <p>How do i implement this using asynctask. i have tried lots of ways that wont work. for now im just reducing the build version to allow network operations on the main thread. what i want to do is retrieve data from a php script that ive hosted and populate the listview. Some help would be highly appreciated :D</p> <pre><code>public class Database extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String url="http://www.mahuwa.com/api/phpserv.php"; String result = null; InputStream is = null; StringBuilder sb = null; ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); List&lt;String&gt; b = new ArrayList&lt;String&gt;(); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch(Exception e){ Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); } //response to inputstream try { BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch(Exception e) { Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); } // to string try{ JSONArray jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i&lt;jArray.length();i++) { json_data = jArray.getJSONObject(i); b.add(json_data.getString("Organization")); } setListAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1,b)); } catch(JSONException e1){ Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show(); } catch (ParseException e1) { Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload