AndroidÖнâÎöÍøÂçÇëÇóµÄURL

來源:互聯網
上載者:User

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£¬

Õâʵ¼ÊÉÏÊÇÒ»¸ö±È½ÏÇåÎúµÄÁ÷³Ì£¬ÆäÖÐÒ²¿ÉÒÔ¿´³ö¶àÏ̴߳¦ÀíµÄģʽ¡£

Ò»µÎÒÃÇÐèÒªÍøÂçÇëÇóµÄʱºò£¬ÎÒÃÇÒ»°ã»á½«ÍøÂçÇëÇóµÄ´¦Àí²¿·Ö·ÅÔÚ×ÓÏß³ÌÖУ¬ÁíÍ⿪һ¸öỊ̈߳¬ÕâÑù¾Í²»»áÔÚÔÏß³ÌÖд¦Àí¹ý¶àµÄÊÂÇ飬ÕâÒ²¼õÇáÁËÖ÷Ï̵߳ÄѹÁ¦¡£

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.