UPDATE: I found that the easiest way is to use AQuery.
I need to get my HttpPost for getting JSON data on another thread and I dont know how to get it all working together. Ive looked at the Painless Threading Blog and still cant get it to work.
Below is my original code befor trying to ansyncing it.
This is my first try at AsyncTasking so plz dont be to harsh. Thnx.
Inside my OnCreate:
/**
* Connecting to MySQL using PHP and Populate the ListView
* ========================================================
*/
mList = (ListView) findViewById(R.id.sidebar_list);
/** - http post for the list - */
try //<--THIS TRY METHOD IS WHAT NEED TO BE ON A SEPERATE THREAD??
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.34/xxxx/xxxxx_list.php");
List<NameValuePair> nameValue = new ArrayList<NameValuePair>();
httpPost.setEntity(new UrlEncodedFormEntity(nameValue));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception e)
{
// TODO handle e
Log.e("log_tag", "Error in HTTP connect" + e.toString());
Toast.makeText(this, "HTTP Connection Error : " + e.toString(), Toast.LENGTH_LONG).show();
}
// EVERYTHING BELOW HERE CAN BE ON THE UI THREAD???
/**
* Convert response to string------------------------------------
* ----------------------
* */
try
{
BufferedReader BufR = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(BufR.readLine() + "\n");
String line = "0";
while ((line = BufR.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e)
{
// TODO handle e
Log.e("log_tag", "Error in convert to String" + e.toString());
}
// paring data
int q_id;
String q_label;
String q_title;
String q_description;
String q_gotoURL;
try
{
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++)
{
json_data = jArray.getJSONObject(i);
q_id = json_data.getInt("_ID");
q_label = json_data.getString("label");
q_title = json_data.getString("title");
q_description = json_data.getString("description");
q_gotoURL = json_data.getString("gotoURL");
// mList.add();
}
setupList();
} catch (JSONException e1)
{
Toast.makeText(getBaseContext(), "No Data Found", Toast.LENGTH_LONG).show();
} catch (ParseException e1)
{
e1.printStackTrace();
}
};
I think you can try something like this: