首先看一个主要的类:
import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import cn.dcbiz.util.Log; public class NetUtil { private static final String TAG = "NetUtil"; /** * 进行网络请求,该方法新起了线程。请求前请实现INetCallbackListener监听 * * @param url * 请求的URL * @param key * 参数的键 * @param values * 参数的值,要与key一一对应 * @param lsn * 网络监听器 */ public static void execute(final String url, final String[] key, final String[] values, final INetCallbackListener lsn) { new Thread() { public void run() { Log.d(TAG, "请求开始:" + url); lsn.onNetStateChange(INetCallbackListener.STATE_SENDING, "正在发送请求"); OutputStream os = null; DataInputStream dis = null; InputStream in = null; HttpConnection conn = null; StringBuffer sb = new StringBuffer(); for (int i = 0; i < values.length; i++) {// 构造请求参数 sb.append(key[i]); sb.append("="); sb.append(values[i]); sb.append("&"); } String body = sb.toString(); body = body.substring(0, body.length() - 1); Log.d(TAG, "参数:" + body); try { conn = (HttpConnection) Connector.open(url); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 不设置就出问题 conn.setRequestProperty("Content-Length", body.getBytes().length + ""); os = conn.openOutputStream(); os.write(body.getBytes("utf-8")); int resCode = conn.getResponseCode(); Log.d(TAG, "resCode:" + resCode); if (resCode == HttpConnection.HTTP_OK) { in = conn.openInputStream(); int len2 = (int) conn.getLength(); dis = new DataInputStream(in); byte[] bs = new byte[len2]; dis.read(bs); String result = new String(bs, "utf-8"); Log.d(TAG, "请求结果:" + result); result = result.substring( result.indexOf("<string>") + 8, result.lastIndexOf('<')); lsn.onNetStateChange(INetCallbackListener.STATE_SENDOVER, result); } else { lsn.onNetStateChange(INetCallbackListener.STATE_SENDERROR, "" + resCode); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); lsn.onNetStateChange(INetCallbackListener.STATE_SENDERROR, e.getMessage()); } finally { try { dis.close(); in.close(); os.close(); conn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start(); } }
说明:public static void execute(final String url, final String[] key, final String[] values, final INetCallbackListener lsn)这个方法是访问WebService的关键方法。
第一个参数是WebService的URL,
第二个参数和第三个参数分别是访问WebService的参数和值,传入时要一一对应,比如访问一个登录接口,要传入:username=denger&password=123456,刚
key = {"username", "password"}; values = {"denger", "123456"};
第三个参数,INetCallbackListener是我写的一个接口,里面只有一个方法,用来做访问结束的回调方法。可以理解为一个监听器,调用都实现它即可。
接口如下:
/** * 网络监听 * * @author Denger * */ public interface INetCallbackListener { /** * 正在发送 */ public static final int STATE_SENDING = 0; /** * 发送结束,但并代表发布的信息成功,得看返回的代码含意 */ public static final int STATE_SENDOVER = 1; /** * 发送出错 */ public static final int STATE_SENDERROR = -1; ////////////////////////// /** * 没有相对应的电话号码 */ public static final String MSG_NO_TEL = "-1"; /** * 发布成功 */ public static final String MSG_PUBLISH_SUC = "true"; /** * 当状态改变时的回调 * * @param state * @param msg * 当状态state为{@link #STATE_SENDOVER}时,msg返回Server返回的JSON字符串 */ void onNetStateChange(int state, String msg); }
调用都实现上面的接口,就可以根据onNetStateChange方法的回调根据网络状态state的不同来做不同的提示或者事件。