SetText from parsed JSON object using AsyncTask
I'm making an android program that parses JSON texts from web page source code on the internet. It works in android 2.2, but I now need it to be on Android 3.0, which should be on AsyncTask. I have experience with AsyncTask, but I'm so confused as to where this would all be. Thanks in advance everyone :)
Here is my method in the MainActivity class:
private void jsonStuffs() {
//JSON PARSER & HOME PAGE TEXTVIEWS
client = new DefaultHttpClient();
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
String jsonStr = test.getInternetData(); //go to GetMethodEx
JSONObject obj = new JSONObject(jsonStr);
//////////////////////find temperature in the JSON in the webpage
String temperature = obj.getString("temperature");
TextView tvTemp = (TextView)findViewById(R.id.textView);
tvTemp.setText(temperature);
}
//catch (JSONException e) {
// e.printStackTrace();
//}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
GetMethodEx class (this will find the link to the web page, then convert the source code to text format):
public class GetMethodEx extends Activity {
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
//
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://nhjkv.comuf.com/json_only.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}finally {
if (in !=null){
try{
in.close();
return data;
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
source to share
You can do something like this (this code is for illustration only, change it as needed)
class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
protected void onPreExecute() {
// You can set your activity to show busy indicator
//setProgressBarIndeterminateVisibility(true);
}
protected JSONObject doInBackground(String... args) {
return jsonStuffs();
}
protected void onPostExecute(final JSONObject jsonObj) {
String temperature = jsonObj.getString("temperature");
TextView tvTemp = (TextView)findViewById(R.id.textView);
tvTemp.setText(temperature);
// Stop busy indicator
//setProgressBarIndeterminateVisibility(false);
}
To call this task use new MyAsyncTask().execute();
(you can pass parameters String
to execute if needed) You can change jsonStuffs()
to returnJSONObject
eg.
private JSONObject jsonStuffs() {
// ...
String jsonStr = test.getInternetData(); //go to GetMethodEx
return new JSONObject(jsonStr);
// ...
}
source to share
It works in android 2.2, but I need it now to be on Android 3.0, which should be in AsyncTask.
=> Yes, it gives NetworkOnMainThreadException in 3.0 if you make a web call without an implementation inside a Thread such as AsyncTask.
I have experience with AsyncTask but I'm so confused this and that.
=> Just include the web call logic inside the doInBackground()
AsyncTask method , in your case call getInternetData () inside doInBackground()
.
FYI, you cannot update the UI directly by doing a long task inside doInBackground (). Yes, if you want to update the interface, follow these steps:
- Update the UI from the onPostExecute () method.
- or implement
runOnUiThread()
insidedoInBackround()
source to share