AsyncTask获取的json数据通过接口返回给Activity

package com.example.internet;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;


import android.os.AsyncTask;


import com.example.model.PersonLizedSData;


/**
 * post请求
 * 
 * @author hlzby
 * 
 */
public class MyInsertActivity extends AsyncTask {


DataDownloadplsListener dataDownloadplsListener;// 接口
public static int GXHFW = 0;// 解析JSON数据时、回调的条件
public static String GXHFW_ID = null;// cat_id--上级查询条件
private List plsList = new ArrayList();
private String id;//
private String send_ren_touxian;// 发送人头衔
private String send_ren;// 发送人
private String task_id;// 任务单号
private String banjietime;// 结束时间
private String task_name;// 任务名称
private String status;// 任务状态
private String send_time;// 发送时间
PersonLizedSData pls_data;


@Override
protected String doInBackground(String... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
// 添加http头信息
httppost.addHeader("Content-Type", "application/json");
JSONObject obj = new JSONObject();
if (GXHFW_ID != null) {
obj.put("cat_id", GXHFW_ID);
obj.put("tablename", "article_gexinghfw");
httppost.setEntity(new StringEntity(obj.toString()));
HttpResponse response;
response = httpclient.execute(httppost);
// int code = response.getStatusLine().getStatusCode();
// if (code == 200) {
// System.err.println("返回值接收");
// }
// 查询返回的值
HttpEntity data = response.getEntity();
InputStream is = data.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
System.out.println("line="+line);
sb.append(line);
return sb.toString();
}
}
dataDownloadplsListener.dataDownloadedplsSuccessfully(plsList);
} catch (Exception e) {
// TODO: handle exception plsList
}
return null;
}


@Override
protected void onPostExecute(String result) {
try {
JSONObject jsResult = new JSONObject(result);
JSONArray ja_content1 = jsResult.getJSONArray("data");// 数组型数据-data-一个元素
JSONObject content;
for (int i = 0; i < ja_content1.length(); i++) {// 遍历data:所有数据
content = (JSONObject) ja_content1.get(i);
System.out.println("content="+content);
id = content.getString("id");//13
send_ren_touxian = content.getString("send_ren_touxian");//null
send_ren = content.getString("send_ren");//""
task_id = content.getString("task_id");//43
banjietime = content.getString("banjietime");
task_name = content.getString("task_name");
status = content.getString("status");
send_time = content.getString("send_time");
// 实例化对象--
pls_data = new PersonLizedSData(id,
send_ren_touxian, send_ren, task_id, banjietime,
task_name, status, send_time);
// 集合添加该对象
plsList.add(pls_data);
}
dataDownloadplsListener.dataDownloadedplsSuccessfully(plsList);
} catch (Exception e) {
// TODO: handle exception
}
super.onPostExecute(result);
}


public MyInsertActivity() {


}


public void setDataDownloadplsListener(
DataDownloadplsListener dataDownloadplsListener) {
this.dataDownloadplsListener = dataDownloadplsListener;
}


public static interface DataDownloadplsListener {
void dataDownloadedplsSuccessfully(List plsList);


void dataDownloadplsFailed();
}


}

-----------------------------------------------------------------------------------------------------------------

private void getData() {
MyAsyncTask.FG_Select = 2;
MyAsyncTask getTask = new MyAsyncTask();
getTask.setDataDownloadListener(new DataDownloadListener() {


@Override
public void dataDownloadedSuccessfully(List msgList) {
for (int i = 0; i < msgList.size(); i++) {
System.err.println(
msgList.get(i).getCat_id() + ","
+ msgList.get(i).getCat_desc() + ","
+ msgList.get(i).getCat_name() + ","
+ msgList.get(i).getFile_url());
cat_id.add(msgList.get(i).getCat_id());//取得id存入集合
list.add(msgList.get(i).getCat_desc());
content.add(msgList.get(i).getCat_name());
list_res.add(msgList.get(i).getFile_url());
}
work_first_view_lv.setAdapter(adapter);
//-
Message msg = handler.obtainMessage();
msg.what = FLAG_DISMISS;
handler.sendMessage(msg);
}


@Override
public void dataDownloadFailed() {


}
});
getTask.execute("http://test3.hnhansan.com/guoshuiapi/?url=db_class&act=search&table=article_gexinghfw_cat");
}

你可能感兴趣的:(Android笔记)