Reputation: 51
I have an app that should parse a json object from a URL and display it into two adjacent listview (the json object itself is a result of a query of MySQL database ) .
I tried with the following code but it seems that I need to AsyncTask class to accomplish this task .
Could someone helps me to write the corresponding AsyncTask class, by guiding me what to write in doinbackground, onPreExecute() ,onPostExecute.
MainActivity
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect();
}
private void connect()
{
String data;
List<String> r = new ArrayList<String>();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
ListView list=(ListView)findViewById(R.id.leftListView);
ListView list2=(ListView)findViewById(R.id.rightListView);
list2.setBackgroundColor(Color.BLUE);
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://192.168.1.16:89/Derdeery/Zaki.php");
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
try {
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
{
JSONObject obj=json.getJSONObject(i);
String name=obj.getString("Part_NAME");
String id = obj.getString("Part_ID") ;
Log.e("name", name);
r.add(name+"-"+id);
list.setAdapter(adapter);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/leftListView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight=".75" />
<ListView
android:id="@+id/rightListView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight=".25" />
</LinearLayout>
Edit: I tried with this code but I get unfortunately app has stopped error.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new GetData().execute();
}
private class GetData extends AsyncTask<Void, Void, Void>
{
ProgressDialog progressDialog;
String data;
List<String> r = new ArrayList<String>();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
ListView list = (ListView)findViewById(R.id.right);
ListView list2= (ListView)findViewById(R.id.left);
public GetData()
{
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(false);
if (!progressDialog.isShowing()) {
progressDialog.show();
}
}
@Override
protected Void doInBackground(Void... params) {
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://192.168.1.5:89/Derdeery/Zaki.php");
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
}
catch (ClientProtocolException e)
{
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
catch (IOException e)
{
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
{
JSONObject obj=json.getJSONObject(i);
String name=obj.getString("Part_NAME");
String id = obj.getString("Part_ID") ;
Log.e("name", name);
r.add(name+"-"+id);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
}
Upvotes: 1
Views: 80
Reputation: 4328
Try like This
private class GetData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
public GetData() {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle(getResources().getString(
R.string.progress_title));
progressDialog.setMessage(getResources().getString(
R.string.progress_message));
progressDialog.setCancelable(false);
if (!progressDialog.isShowing()) {
progressDialog.show();
}
}
@Override
protected Void doInBackground(Void... params) {
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://192.168.1.16:89/Derdeery/Zaki.php");
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
} catch (ClientProtocolException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
{
JSONObject obj=json.getJSONObject(i);
String name=obj.getString("Part_NAME");
String id = obj.getString("Part_ID") ;
Log.e("name", name);
r.add(name+"-"+id);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 1360
I can give you a rough idea:
1.) Perform the network operation i.e. fetching the data from your database (on server side, converting it into json etc) inside doInBackGround().
2.) The doInBackground() will return a result, which will be received by onPostExecute(), inside onPostExecute, set that result on the 2 listviews.
3.) The onPreExecute() will contain the progress dialog logic which will be shown when doInBackground will be running.
Hope you can try once. Let me know otherwise, I will help you further.
Upvotes: 1