我有一個應用程序應該從一個URL解析一個json對象並將其顯示到兩個相鄰的listview(json對象本身是MySQL數據庫查詢的結果)。顯示json數據到兩個相鄰的列表視圖
我試着用下面的代碼,但似乎我需要AsyncTask類來完成這項任務。
有人可以幫我編寫相應的AsyncTask類,通過指導我在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>
編輯:我想這個代碼,但我得到不幸的應用程序已經停止錯誤。
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();
}
}}
}
請參閱http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html –