AndroidÖнâÎöÍøÂçÇëÇóµÄURL
×î½üÕýÔÚ×öAndroidÍøÂçÓ¦ÓõĿª·¢£¬Ê¹ÓÃÁËandroidÍøÂçÇëÇó·½ÃæµÄ֪ʶ£¬ÏÖÔÚÏò´ó¼Ò½éÉÜÍøÂçÇëÇó·½ÃæµÄ֪ʶ£¬ÎÒÃÇÖªµÀandroidÖÐÏò·þÎñÆ÷¶Ë·¢ËÍÒ»¸öÇëÇ󣬣¨Õâ¾ÍÊÇÎÒÃÇͨ³£Ëù˵µÄPOSTÇëÇ󣣬ÎÒÃÇÒª·¢ËÍÒ»¸öÍêÕûµÄURL£¬È»ºó·þÎñÆ÷¶Ë½ÓÊÕµ½Õâ¸öURL£¬¶ÔÕâ¸öURL½øÐÐÌØ¶¨µÄ½âÎö£¬¾ÍÊǶÔURL½øÐнâÎö£¬×ª»¯ÎªJSONÊý¾Ý£¬È»ºó£¬ÎÒÃÇÖ»Òª´¦ÀíÕâ¸öJSONÊý¾Ý¾Í¿ÉÒÔÁË¡£
ÎÒÏÖÔÚ¾ÍÓÃÎÒµÄÏîĿʵÀýÀ´ÌåÏÖ½âÎöURLµÄ¹ý³Ì£º
1¡¢×é×°URLµÄ¹ý³Ì£º<†·Ÿ"http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">private String getOrderPayUrl(int order, int action, String accountid,String token) {Calendar calendar = Calendar.getInstance();long time = calendar.getTimeInMillis() / 1000;return orderPayUrl + "?action=" + action + "&time=" + time+ "&accountid=" + accountid + "&token=" + token + "&paymoney="+ order + "¤cy=CNY&" + "sign="+ getSign(action, time, accountid);}
2¡¢·¢ËÍURLµÄ¹ý³Ì£º
private void httpRequest(String url, int which, String method) {HttpRequestTask task = new HttpRequestTask(mHandler, url, which, method);task.startTask();}
ÆäÖÐmHandlerΪһ¸ö¶¨ÒåµÄ¾Ö²¿±äÁ¿£¬ÓÃHandlerÀàÐÍÀ´´¦Àí·µ»ØµÄ½âÎö½á¹û£¬
public class HttpRequestTask implements Runnable {private Handler handler;private String url;private int which;private String method;public HttpRequestTask(Handler handler, String url, int which, String method) {this.url = url;this.handler = handler;this.which = which;this.method = method;}public void startTask() {new Thread(this).start();}@Overridepublic void run() {Looper.prepare();sendRequest();}private void sendRequest() {String result = null;if (method != null && method.equals(MyConstant.POST)) {result = HttpUtil.queryStringForPost(url);}if (method != null && method.equals(MyConstant.GET)) {result = HttpUtil.queryStringForGet(url);}// Log.e("---result---", result);Message msg = Message.obtain();msg.what = which;msg.obj = result;handler.sendMessage(msg);}}
3¡¢½âÎöURLµÄ¹ý³Ì£º
// ·¢ËÍPostÇëÇ󣬻ñµÃÏìÓ¦²éѯ½á¹ûpublic static String queryStringForPost(String url) {HttpPost request = HttpUtil.getHttpPost(url);String result = null;try {// »ñµÃÏìÓ¦¶ÔÏóHttpResponse response = HttpUtil.getHttpResponse(request);if (response.getStatusLine().getStatusCode() == 200) {result = EntityUtils.toString(response.getEntity());return result;}} catch (ClientProtocolException e) {e.printStackTrace();result = "ÍøÂçÒì³££¡";return result;} catch (IOException e) {e.printStackTrace();result = "ÍøÂçÒì³££¡";return result;}return result;}
// // »ñµÃpostÇëÇó¶ÔÏórequestpublic static HttpPost getHttpPost(String url) {// È¥³ý¿Õ¸ñ//if (url != null) {//Pattern p = Pattern.compile("\\s");//Matcher m = p.matcher(url);//url = m.replaceAll("");//}HttpPost request = new HttpPost(url);return request;}
ÆäÖÐÎÒÃÇҪʹÓõİüÎļþÊÇ
org.apache.http.client.methods.HttpPost
ʵ¼ÊÉÏ·µ»ØµÄresult×Ö·û´ÊÇÒ»¸öJSONÀàÐ͵Ä×Ö·û´£¬ÎÒÃÇÖ»ÐèҪʹÓÃJSONObjectÀ´´¦ÀíÏàÓ¦µÄJSON¾Í¿ÉÒÔÁË£¬µÃµ½ÎÒÃÇÐèÒªÊý¾Ý£¬·µ»Ø£¬OK£¬
Õâʵ¼ÊÉÏÊÇÒ»¸ö±È½ÏÇåÎúµÄÁ÷³Ì£¬ÆäÖÐÒ²¿ÉÒÔ¿´³ö¶àÏ̴߳¦ÀíµÄģʽ¡£
Ò»µÎÒÃÇÐèÒªÍøÂçÇëÇóµÄʱºò£¬ÎÒÃÇÒ»°ã»á½«ÍøÂçÇëÇóµÄ´¦Àí²¿·Ö·ÅÔÚ×ÓÏß³ÌÖУ¬ÁíÍ⿪һ¸öỊ̈߳¬ÕâÑù¾Í²»»áÔÚÔÏß³ÌÖд¦Àí¹ý¶àµÄÊÂÇ飬ÕâÒ²¼õÇáÁËÖ÷Ï̵߳ÄѹÁ¦¡£