GetContent

来源:互联网 发布:windows 10 14393 编辑:程序博客网 时间:2024/06/13 05:18
public class GetContent extends Thread {private static final String TAG = "GetContent";private GetContentListener listener;private String urlString;private static String sessionid; public static boolean isBreak = false;public GetContent(String string) {urlString = Content.SERVICE_URL + string;}private Handler handler = new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);switch (msg.what) {case 0:listener.onSucceed((String) msg.obj);break;case 1:listener.onFailed((String) msg.obj);break;default:break;}}};public synchronized void setGetContentListener(GetContentListener contentListener) {listener = contentListener;}public void setUrlString(String string) {urlString = Content.SERVICE_URL + string;}@Overridepublic void run() {// TODO Auto-generated method stubsuper.run();isBreak = false;new Runnable() {public synchronized void run() {URL url;HttpURLConnection connection = null;try {url = new URL(urlString);connection = (HttpURLConnection) url.openConnection();if(sessionid != null) { connection.setRequestProperty("cookie", sessionid); }else{String cookieval = connection.getHeaderField("set-cookie");if(cookieval != null) { sessionid = cookieval.substring(0, cookieval.indexOf(";")); }}connection.setConnectTimeout(Content.CONNECT_TIMEOUT);connection.setReadTimeout(Content.CONNECT_TIMEOUT);InputStreamReader streamReader = new InputStreamReader(connection.getInputStream(), "utf-8");BufferedReader reader = new BufferedReader(streamReader);if (connection.getResponseCode() == 200) {StringBuffer buffer = new StringBuffer();String line = "";while ((line = reader.readLine()) != null) {buffer.append(line);}JSONObject object = new JSONObject(buffer.toString());checkContent(object);} else {transfrom("服务器连接失败!", 1);}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blockif(sessionid == null) transfrom("请输入正确验证码", 1);else transfrom("服务器连接失败!", 1);e.printStackTrace();} finally {if (connection != null)connection.disconnect();}}}.run();}public void checkContent(JSONObject object) {try {int code = object.getInt("status");if (code == 200) {String success = object.getString("data");transfrom(success, 0);} else {String faile = object.getString("message");transfrom(faile, 1);}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public synchronized void transfrom(String string, int what) {if(isBreak) return;Message message = new Message();message = new Message();message.obj = string;message.what = what;handler.sendMessage(message);}public void setIsBreak(boolean breakd){isBreak = breakd;}public interface GetContentListener {public void onSucceed(String success);public void onFailed(String faile);}}



Activity使用

GetContent getContent = new GetContent("cattlePeople/list?pageIndex=" + page);getContent.start();getContent.setGetContentListener(listener);private GetContentListener listener = new GetContentListener() {@Overridepublic void onSucceed(String success) {// TODO Auto-generated method stubtry {JSONArray array = new JSONArray(success);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void onFailed(String faile) {// TODO Auto-generated method stubif(!faile.equals("暂无数据")) Toast.makeText(getActivity(), "网络连接失败,请检查网络", Toast.LENGTH_SHORT).show();}};


0 0
原创粉丝点击