首页 > 代码库 > Android四大组件之Service(续2)
Android四大组件之Service(续2)
1.HttpRequest
package com.joyfulmath.android4example.http;import java.io.BufferedReader;import java.io.InputStreamReader;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import com.joyfulmath.android4example.R;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class HttpDemoActivity extends Activity implements IResult { protected static final String TAG = "httpdemo.HttpDemoActivity"; public Handler mMainHandler; public static final String RequestUrl = "http://www.baidu.com"; Button mBtnRequest; TextView mResponseView; Button mbtn_json; /* * (non-Javadoc) * * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_httpdemo_main); mBtnRequest = (Button) this.findViewById(R.id.btn_request); mResponseView = (TextView) this.findViewById(R.id.httpdemo_text); mbtn_json = (Button) this.findViewById(R.id.btn_json); mBtnRequest.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { WorkThread work = new WorkThread(HttpDemoActivity.this, RequestUrl); work.start(); } }); mbtn_json.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { JSonParser parser = new JSonParser(); parser.parser(); } }); mMainHandler = new Handler() { /* * (non-Javadoc) * * @see android.os.Handler#handleMessage(android.os.Message) */ @Override public void handleMessage(Message msg) { String str = (String) msg.obj; Log.i(TAG, "get " + str); mResponseView.setText(str); } }; } /* * (non-Javadoc) * * @see android.app.Activity#onStart() */ @Override protected void onStart() { super.onStart(); } /* * (non-Javadoc) * * @see android.app.Activity#onStop() */ @Override protected void onStop() { super.onStop(); } /* * (non-Javadoc) * * @see android.app.Activity#onDestroy() */ @Override protected void onDestroy() { super.onDestroy(); } private class WorkThread extends Thread { IResult mListener; String address; String message = ""; public WorkThread(IResult result, String address) { mListener = result; this.address = address; } /* * (non-Javadoc) * * @see java.lang.Thread#run() */ @Override public void run() { Log.i(TAG, "WorkThread start"); try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(address); HttpResponse response = client.execute(request); InputStreamReader inreader = new InputStreamReader(response .getEntity().getContent()); BufferedReader reader = new BufferedReader(inreader); String str = reader.readLine(); Log.i(TAG, "WorkThread start str:"+str); while(str!=null) { message+= str; str = reader.readLine(); } if(mListener!=null) { mListener.onResult(message); } } catch (Exception ex) { Log.i(TAG, "WorkThread ex:"+ex.getMessage()); } } } @Override public void onResult(String msg) { Message message = new Message(); message.what = 1; message.obj = msg; mMainHandler.sendMessage(message); }}
package com.joyfulmath.android4example.http;public interface IResult { void onResult(String msg);}
json格式解析:
package com.joyfulmath.android4example.http;import org.json.JSONArray;import org.json.JSONObject;import org.json.JSONTokener;import android.util.Log;public class JSonParser { private static final String JSON = "{" + " \"phone\" : [\"12345678\", \"87654321\"]," + " \"name\" : \"yuanzhifei89\"," + " \"age\" : 100," + " \"address\" : { \"country\" : \"china\", \"province\" : \"jiangsu\" }," + " \"married\" : false" + "}"; private static final String TAG = "httpdemo.JSonParser"; public void parser() { Log.i(TAG, "[parser]"); try{ JSONTokener jsonparser = new JSONTokener(JSON); JSONObject person = (JSONObject) jsonparser.nextValue(); JSONArray pbones = person.getJSONArray("phone"); Log.i(TAG, "[parser]pbones:"+pbones.getString(0)+" "+pbones.getString(1)); String name = person.getString("name"); Log.i(TAG, "[parser]name:"+name); }catch(Exception ex) { Log.i(TAG, "[parser]ex:"+ex.getMessage()); } }}
xml解析:
三种:DOM,SAX,PULL
android 常用的xmlpullreader:
package com.joyfulmath.android4example.http;import java.io.IOException;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import com.joyfulmath.android4example.R;import android.content.res.Resources;import android.content.res.XmlResourceParser;import android.util.Log;public class XmlParser { private static final String TAG = "httpdemo.XmlParser"; // pull /*xmlpullparser 不关心xml的结构属性,它就是开始,结束,属性,value。 * * */ public void pullXml(Resources res) { Log.i(TAG, "[pullXml]"); XmlResourceParser parser = res.getXml(R.layout.layout_httpdemo_main);// xmlpullparser int eventType; try { eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_DOCUMENT) { Log.i(TAG, "[pullXml] START_DOCUMENT"); } else if (eventType == XmlPullParser.START_TAG) { Log.i(TAG, "[pullXml] START_TAG:" + parser.getName()); int count = parser.getAttributeCount(); Log.i(TAG, "[pullXml] \t count" + count ); for (int i = 0; i < count; i++) { String noteName = parser.getAttributeName(i); Log.i(TAG, "[pullXml] \t noteName:" + noteName + " at " + i); if(noteName.equals("id")||noteName.equals("text")) { String value = parser.getAttributeValue(i); Log.i(TAG, "[pullXml]\t noteValue:" + value + " at " + i); } } } else if (eventType == XmlPullParser.END_TAG) { Log.i(TAG, "[pullXml] END_TAG:" + parser.getName()); } else if (eventType == XmlPullParser.TEXT) { Log.i(TAG, "[pullXml] TEXT:" + parser.getText()); } eventType = parser.next(); } Log.i(TAG, "[pullXml] END_DOCUMENT"); } catch (XmlPullParserException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Android四大组件之Service(续2)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。