很久没有码字了,今天跟大家分享一个模拟get和post方法的工具类,在安卓应用中很多都需要跟服务器进行数据交互,这需要两方面的配合,首先服务器端会给应用提供一些数据交互的接口,可是怎样在应用中去调用呢?这就需要用到get和post方法了,下面是自己总结并且一直在用的两个工具类,请看代码:
用来模拟get方法:
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; } } }
用来数据回调的接口:
package com.standopen.exceptionsdk; public interface thread_callback { public void callback(String back); }
从数据库中获取到数据后,就要对数据进行解析,提取到需要的数据进行使用,这些也非常简单,下次再分享。