好久沒有碼字了,今天跟你們分享一個模擬get和post方法的工具類,在安卓應用中不少都須要跟服務器進行數據交互,這須要兩方面的配合,首先服務器端會給應用提供一些數據交互的接口,但是怎樣在應用中去調用呢?這就須要用到get和post方法了,下面是本身總結而且一直在用的兩個工具類,請看代碼:java
用來模擬get方法:android
package com.standopen.exceptionsdk; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.content.Context; import android.os.Handler; import android.os.Message; public class doGet { private Context ctx; private String url; private thread_callback callback; public doGet(Context ctx, String url, thread_callback back) { this.ctx = ctx; this.url = url; this.callback = back; post_thread th = new post_thread(ctx, url); th.start(); } public class post_thread extends Thread { private String url; private Context ctx; public post_thread(Context ctx, String url) { this.url = url; this.ctx = ctx; } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); // int type = Integer.parseInt(msg.obj.toString()); String back = msg.obj.toString(); callback.callback(back); } }; @Override public void run() { // TODO Auto-generated method stub super.run(); String str = null; try { str = post(url); Message msg1 = handler.obtainMessage(); msg1.obj = str; handler.sendMessage(msg1); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public synchronized void start() { // TODO Auto-generated method stub super.start(); } public String post(String url) throws ClientProtocolException, IOException { String str = ""; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); int code = response.getStatusLine().getStatusCode(); if (code == 200) { InputStream is = response.getEntity().getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; if ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } byte[] b = baos.toByteArray(); is.close(); baos.close(); str = new String(b, "utf-8"); } return str; } } }
用來模擬post方法:數據庫
package com.standopen.exceptionsdk; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import android.content.Context; import android.os.Handler; import android.os.Message; public class doPost { private Context ctx; private String url; private thread_callback callback; private ArrayList<NameValuePair> data = null; public doPost(Context ctx, String url, ArrayList<NameValuePair> data, thread_callback back) { this.ctx = ctx; this.url = url; this.callback = back; this.data = data; post_thread th = new post_thread(ctx, url, data); th.start(); } public class post_thread extends Thread { private String url; private Context ctx; private ArrayList<NameValuePair> data; public post_thread(Context ctx, String url, ArrayList<NameValuePair> data) { this.url = url; this.ctx = ctx; this.data = data; } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); // int type = Integer.parseInt(msg.obj.toString()); String back = msg.obj.toString(); callback.callback(back); } }; @Override public void run() { // TODO Auto-generated method stub super.run(); String str = null; try { str = post(url); Message msg1 = handler.obtainMessage(); msg1.obj = str; handler.sendMessage(msg1); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public synchronized void start() { // TODO Auto-generated method stub super.start(); } public String post(String url) throws ClientProtocolException, IOException { String str = ""; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setEntity(new UrlEncodedFormEntity(data, "utf-8")); HttpResponse response = client.execute(post); int code = response.getStatusLine().getStatusCode(); if (code == 200) { InputStream is = response.getEntity().getContent(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; if ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } byte[] b = baos.toByteArray(); is.close(); baos.close(); str = new String(b); } return str; } } }
用來數據回調的接口:apache
package com.standopen.exceptionsdk; public interface thread_callback { public void callback(String back); }
從數據庫中獲取到數據後,就要對數據進行解析,提取到須要的數據進行使用,這些也很是簡單,下次再分享。服務器