˵Ã÷: okhttpÊÇ°²×¿Öг£ÓõIJå¼þ,ËٶȿìÐÔÄÜÎȶ¨£¬ºÍhttpÄ£¿éÏศÏà³É£¬ÌṩÁËһЩhttpÄ£¿é²»¾ß±¸µÄ·½·¨ÀýÈçjsonÌá½»ºÍÉÏ´«ÏÂÔØÎļþµÈµÚÒ»¸ö£º
ÏëҪʹÓÃgetCookie() ·½·¨»ñÈ¡cookie¡£¼ÙÉèÄúÏëÔÚokHttp µÄÉÏÏÂÎÄÖÐʹÓô˷½·¨£¬¿ÉÒÔ½«ÆäÖ±½ÓÌí¼Óµ½ÄúµÄ´úÂëÖС£ÒÔÏÂÊÇÒ»¸öʾÀý£¬Õ¹Ê¾ÁËÈçºÎʹÓÃgetCookie() ·½·¨»ñÈ¡cookie²¢Êä³ö£º
var okhttp1 = new okHttp();
var cookie = okhttp1.getCookie();
printl(cookie);
Ïêϸ˵Ã÷- ´´½¨
okHttp ʵÀý£ºÊ¹ÓÃvar ¹Ø¼ü×Ö´´½¨Ò»¸öokHttp ʵÀý¡£ - »ñÈ¡cookie£ºµ÷ÓÃ
getCookie() ·½·¨À´»ñÈ¡µ±Ç°µÄcookie¡£ - Êä³ö½á¹û£ºÊ¹ÓÃ
printl ·½·¨½«cookieÊä³öµ½¿ØÖÆ̨¡£
Èç¹ûÄúÐèÒªÔÚ·¢ËÍÇëÇó֮ǰÉèÖÃcookie£¬Äú¿ÉÒÔʹÓÃsetCookie ·½·¨¡£ÀýÈ磺
var okhttp1 = new okHttp();
okhttp1.setHeader('token', 'ÄúµÄtokenÖµ');
okhttp1.setHeader('user-agent', 'ÄúµÄUser-Agent');
var cookie = okhttp1.getCookie();
okhttp1.setCookie('cookie_name', cookie);
µÚ¶þ¸ö£º Äú¿ÉÒÔʹÓÃsetCookie(String arg0, String arg1) ·½·¨Ìí¼Ócookie¡£arg0 ÊÇcookieµÄÃû³Æ£¬arg1 ÊÇcookieµÄÖµ¡£ ÒÔÏÂÊÇÒ»¸öʾÀý£¬Õ¹Ê¾ÁËÈçºÎʹÓÃsetCookie ·½·¨£º
var okhttp1 = new okHttp();
var cookieName = 'your_cookie_name';
var cookieValue = 'your_cookie_value';
okhttp1.setCookie(cookieName, cookieValue);
printl('CookieÒÑÉèÖÃ: ' + cookieName + '=' + cookieValue);
Ïêϸ˵Ã÷- ´´½¨
okHttp ʵÀý£ºÊ¹ÓÃvar ¹Ø¼ü×Ö´´½¨Ò»¸öokHttp ʵÀý¡£ - ÉèÖÃcookie£ºµ÷ÓÃ
setCookie ·½·¨£¬´«ÈëcookieµÄÃû³ÆºÍ¶ÔÓ¦µÄÖµ¡£ - Êä³ö½á¹û£ºÊ¹ÓÃ
printl ·½·¨Êä³öÉèÖõÄcookieÐÅÏ¢¡£
Äú¿ÉÒÔÔÚ·¢ËÍÇëÇó֮ǰÉèÖÃcookie£¬ÕâÑù¿ÉÒÔÈ·±£ÇëÇóÖаüº¬ËùÐèµÄcookieÐÅÏ¢¡£Èç¹ûÄúÓÐÐèÒªÉèÖõÄʵ¼ÊcookieÃû³ÆºÍÖµ£¬ÇëÌ滻ʾÀýÖеÄռλ·û¡£ µÚÈý¸ö£º
¿ÉÒÔʹÓÃgetResponseHeaders() ·½·¨»ñÈ¡HTTPÏìӦͷ¡£ÏÂÃæÊÇÒ»¸öÍêÕûµÄʾÀý£¬Õ¹Ê¾ÁËÈçºÎÔÚokHttp ÖÐʹÓø÷½·¨£º
var http1 = new okHttp();
http1.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Mobile Safari/537.36");
var res = http1.get("https://v.kuaishou.com/nQtui");
var responseHeaders = http1.getResponseHeaders();
printl(responseHeaders);
Ïêϸ˵Ã÷- ´´½¨
okHttp ʵÀý£ºÊ¹ÓÃvar ¹Ø¼ü×Ö´´½¨Ò»¸öokHttp ʵÀý¡£ - ÉèÖÃÇëÇóÍ·£ºÊ¹ÓÃ
setHeader ·½·¨ÉèÖÃÐèÒªµÄÇëÇóÍ·£¬ÀýÈçUser-Agent ¡£ - ·¢ËÍGETÇëÇó£ºÊ¹ÓÃ
get ·½·¨·¢ËÍHTTP GETÇëÇóµ½Ö¸¶¨µÄURL¡£ - »ñÈ¡ÏìӦͷ£ºµ÷ÓÃ
getResponseHeaders ·½·¨»ñÈ¡ÏìӦͷ¡£ - Êä³öÏìӦͷ£ºÊ¹ÓÃ
printl ·½·¨Êä³ö»ñÈ¡µÄÏìӦͷÐÅÏ¢¡£
ͨ¹ýÕâ¸öÀý×Ó£¬Äú¿ÉÒÔ»ñÈ¡HTTPÏìÓ¦µÄËùÓÐÍ·ÐÅÏ¢²¢ÔÚ¿ØÖÆ̨Êä³ö¡£¸ù¾Ýʵ¼ÊÇé¿öµ÷ÕûÏà¹ØµÄURL»òÇëÇóÍ·¡£ µÚËĸö£º Äú¿ÉÒÔʹÓÃsetHeader(String arg0, String arg1) ·½·¨À´ÉèÖÃHTTPÇëÇóÍ·¡£arg0 ÊDzÎÊýÃû³Æ£¨ÀýÈçtoken £©£¬arg1 ÊDzÎÊýÖµ£¨ÀýÈç12345667 £©¡£ ÒÔÏÂÊÇʹÓÃsetHeader ·½·¨µÄʾÀý´úÂ룺
var http1 = new okHttp();
http1.setHeader("token", "12345667");
http1.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");
Ïêϸ˵Ã÷- ´´½¨
okHttp ʵÀý£ºÊ¹ÓÃvar ¹Ø¼ü×Ö´´½¨Ò»¸öokHttp ʵÀý¡£ - ÉèÖÃHTTPÍ·£ºÊ¹ÓÃ
setHeader ·½·¨ÉèÖòÎÊýÃû³ÆºÍ²ÎÊýÖµ£¬ÀýÈçtoken ºÍ¶ÔÓ¦µÄÖµ¡£ - ·¢ËÍÇëÇó£ºÉèÖÃÍêÇëÇóÍ·ºó£¬¿ÉÒÔ·¢ËÍHTTPÇëÇó²¢½øÐкóÐø´¦Àí¡£
È·±£½«Ê¾ÀýÖеIJÎÊýÃû³ÆºÍÖµÌ滻ΪÄúʵ¼ÊÐèÒªµÄÄÚÈÝ¡£Èç¹ûÐèÒªÉèÖöà¸öÇëÇóÍ·£¬¿ÉÒÔ¶à´Îµ÷ÓÃsetHeader ·½·¨À´Íê³É¡£ µÚËĸö£º Äú¿ÉÒÔʹÓÃclearHeader() ·½·¨À´Çå³ýµ±Ç°µÄHTTPÇëÇóÍ·¡£ÕâÔÚÐèÒªÖØÖÃÇëÇóÍ·»òÔÚ·¢ËͶà¸öÇëÇóʱ·Ç³£ÓÐÓᣠÒÔÏÂÊÇÈçºÎʹÓÃclearHeader() ·½·¨µÄʾÀý´úÂ룺
var http1 = new okHttp();
http1.setHeader("token", "12345667");
http1.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");
http1.clearHeader();
http1.setHeader("New-Header", "newValue");
Ïêϸ˵Ã÷- ´´½¨
okHttp ʵÀý£ºÊ¹ÓÃvar ¹Ø¼ü×Ö´´½¨Ò»¸öokHttp ʵÀý¡£ - ÉèÖÃÇëÇóÍ·£ºÊ¹ÓÃ
setHeader ·½·¨ÉèÖóõʼÇëÇóÍ·¡£ - Çå³ýÇëÇóÍ·£ºµ÷ÓÃ
clearHeader() ·½·¨Çå³ýËùÓе±Ç°µÄÇëÇóÍ·¡£ - ÖØÐÂÉèÖÃÇëÇóÍ·£ºÔÚÇå³ýºó£¬¿ÉÒÔÖØÐÂÉèÖÃеÄÇëÇóÍ·¡£
- ·¢ËÍÇëÇó£ºÉèÖÃÍêÇëÇóÍ·ºó£¬¿ÉÒÔ·¢ËÍHTTPÇëÇó²¢½øÐкóÐø´¦Àí¡£
ͨ¹ýÕâÖÖ·½Ê½£¬Äú¿ÉÒÔÁé»î¹ÜÀíHTTPÇëÇóÍ·£¬È·±£ËüÃÇÔÚÿ´ÎÇëÇóÖзûºÏÄúµÄÐèÇó¡£ µÚÎå¸ö£º
downloadFile ·½·¨ÓÃÓÚÏÂÔØÎļþ£¬½ÓÊÜÁ½¸ö²ÎÊý£º
arg0 : URL£¨ÒªÏÂÔØÎļþµÄµØÖ·£©arg1 : Îļþ·¾¶£¨±£´æÎļþµÄ±¾µØ·¾¶£©
ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룬չʾÈçºÎʹÓÃdownloadFile ·½·¨£º
var okhttp1 = new okHttp();
okhttp1.setHeader('token', 'ÄúµÄToken');
var fileUrl = 'http://example.com/path/to/your/file.zip';
var localPath = '/sdcard/Download/file.zip';
var downloadResponse = okhttp1.downloadFile(fileUrl, localPath);
printl(downloadResponse);
Ïêϸ˵Ã÷- ´´½¨ÊµÀý£ºÍ¨¹ý
new okHttp() ´´½¨okHttp ʵÀý¡£ - ÉèÖÃÇëÇóÍ·£º¸ù¾ÝÐèÒªÉèÖÃÇëÇóÍ·£¬Èç¹ûÐèÒªÉí·ÝÑéÖ¤µÈ¡£
- Ö¸¶¨ÎļþURLºÍ±£´æ·¾¶£ºÌ滻Ϊʵ¼ÊÏÂÔصÄÎļþURLºÍ±¾µØ±£´æ·¾¶¡£
- µ÷ÓÃ
downloadFile ·½·¨£ºÊ¹ÓÃdownloadFile ·½·¨ÏÂÔØÎļþ£¬²¢´æ´¢·µ»Ø½á¹û¡£ - Êä³ö½á¹û£ºÊ¹ÓÃ
printl Êä³öÏÂÔؽá¹û¡£
×¢ÒâÊÂÏî- È·±£ÓÐÕýÈ·µÄȨÏÞÔÚÖ¸¶¨µÄ·¾¶ÉÏдÈëÎļþ¡£
- URL±ØÐëÓÐЧÇÒ¿ÉÒÔ·ÃÎÊ£¬ÒÔÈ·±£ÏÂÔسɹ¦¡£
Çë¸ù¾ÝÄúµÄʵ¼ÊÐèÇóÓë»·¾³µ÷Õû´úÂëƬ¶Î¡£ µÚÁù¸ö£º
downloadFileProgress ·½·¨ÓÃÓÚÏÂÔØÎļþ²¢ÏÔʾÏÂÔؽø¶È¡£Ëü½ÓÊÜÒÔϲÎÊý£º - arg0£ºURL£¨ÒªÏÂÔØÎļþµÄµØÖ·£©
- arg1£ºÎļþ·¾¶£¨±¾µØ±£´æÎļþµÄ·¾¶£©
- arg2£º»Øµ÷º¯Êý£¨ÓÃÓÚ¸ú×ÙÏÂÔؽø¶È£©
ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓÃdownloadFileProgress ·½·¨µÄʾÀý´úÂ룺
var fileUrl = "http://example.com/path/to/your/file.zip";
var localPath = "/sdcard/Download/file.zip";
function progressCallback(percentage) {
printl("ÏÂÔؽø¶È: " + percentage + "%");
}
var response = downloadFileProgress(fileUrl, localPath, progressCallback);
printl(response);
´úÂë˵Ã÷- ÎļþURLºÍ±£´æ·¾¶£º½«ÎļþURLºÍÏÂÔر£´æµÄ·¾¶Ì滻ΪÄúÐèÒªµÄÄÚÈÝ¡£
- »Øµ÷º¯Êý£º
progressCallback º¯Êý½«½ÓÊÕÏÂÔصĽø¶È°Ù·Ö±È£¬²¢Í¨¹ýprintl Êä³ö¡£ - µ÷Ó÷½·¨£ºÊ¹ÓÃ
downloadFileProgress ·½·¨½øÐÐÎļþÏÂÔØ£¬²¢´«ÈëÏàÓ¦µÄ²ÎÊý¡£
×¢ÒâÊÂÏî- È·±£ÍøÂçÁ¬½ÓÕý³£ÇÒÄ¿±êÎļþURLÓÐЧ¡£
- È·±£Ó¦ÓóÌÐò¾ßÓзÃÎʴ洢ȨÏÞ£¬ÒԱ㱣´æÏÂÔØÎļþ¡£
µÚÁù¸ö£º
get ·½·¨ÓÃÓÚ·¢ËÍGETÇëÇ󣬽ÓÊÜÒ»¸öURL×÷Ϊ²ÎÊý£¬²¢·µ»ØÒ»¸ö×Ö·û´®ÏìÓ¦¡£
ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓÃget ·½·¨µÄʾÀý´úÂ룺
var url = "http://kg.lbzyw.top/api/banks/collect";
var response = http.get(url);
printl(response);
´úÂë˵Ã÷- URLÉèÖ㺽«ÇëÇóµÄURLÌ滻ΪÄúµÄʵ¼ÊÄ¿±êµØÖ·¡£
- ·¢ËÍÇëÇó£ºÊ¹ÓÃ
http.get(url) ·¢ËÍÇëÇó¡£ - Êä³öÏìÓ¦£º½«ÏìÓ¦½á¹ûͨ¹ý
printl Êä³ö¡£
×¢ÒâÊÂÏî- È·±£URLÓÐЧ²¢ÇÒ¿ÉÒÔ·ÃÎÊ¡£
- ¼ì²éÍøÂçÁ¬½ÓÇé¿ö£¬ÒÔÈ·±£Äܹ»³É¹¦·¢ËÍÇëÇó¡£
µÚÆ߸ö£º
getCookie ·½·¨ÓÃÓÚ»ñÈ¡µ±Ç°µÄCookie£¬²¢·µ»ØÒ»¸ö×Ö·û´®¡£ ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓÃgetCookie ·½·¨µÄʾÀý´úÂ룺
var cookie = getCookie();
printl(cookie);
´úÂë˵Ã÷- µ÷ÓÃ
getCookie ·½·¨£ºÖ±½Óµ÷Óø÷½·¨ÒÔ»ñÈ¡µ±Ç°µÄCookie×Ö·û´®¡£ - Êä³öCookieÖµ£ºÊ¹ÓÃ
printl º¯ÊýÊä³ö»ñµÃµÄCookieÖµ¡£
×¢ÒâÊÂÏî- È·±£ÔÚºÏÊʵÄÉÏÏÂÎÄÖе÷Óô˷½·¨£¬Í¨³£ÊÇÔÚÒѽ¨Á¢µÄ»á»°ºó¡£
- Êä³öµÄCookie¿ÉÄÜ°üº¬¶à¸ö¼üÖµ¶Ô£¬Çë¸ù¾ÝÐèÒª½øÐнâÎö¡£
µÚ°Ë¸ö£º
getResponseHeaders ·½·¨ÓÃÓÚ»ñÈ¡HTTPÏìӦͷ£¬²¢·µ»ØÒ»¸ö×Ö·û´®¡£ÒÔÏÂÊÇÈçºÎʹÓÃÕâ¸ö·½·¨µÄʾÀý´úÂë¡£
ʾÀý´úÂë
var http1 = new okHttp();
http1.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Mobile Safari/537.36");
var res = http1.get("https://v.kuaishou.com/nQtuiI");
var headers = http1.getResponseHeaders();
printl(headers);
´úÂë˵Ã÷- ´´½¨okHttpʵÀý£ºÍ¨¹ý
new okHttp() À´´´½¨Ò»¸öHTTPʵÀý¡£ - ÉèÖÃÇëÇóÍ·£ºÊ¹ÓÃ
setHeader ·½·¨ÉèÖÃUser-Agent¡£ - ·¢ËÍGETÇëÇ󣺵÷ÓÃ
http1.get() ·¢ËÍÇëÇó¡£ - »ñÈ¡ÏìӦͷ£ºµ÷ÓÃ
http1.getResponseHeaders() »ñÈ¡HTTPÏìӦͷ£¬²¢½«ÆäÊä³ö¡£
×¢ÒâÊÂÏî- È·±£URLÊÇÓÐЧµÄ£¬²¢ÇÒ¿ÉÒÔ´ÓÄúµÄÉ豸·ÃÎÊ¡£
- Êä³öµÄÏìӦͷ¿ÉÄÜ»á°üº¬¶à¸ö¼üÖµ¶Ô£¬Äú¿ÉÒÔ¸ù¾ÝÐèÒª½øÒ»²½´¦Àí»ò½âÎö¡£
µÚ¾Å¸ö£º
json ·½·¨ÓÃÓÚ·¢ËÍJSON¸ñʽµÄÊý¾Ý¡£Ëü½ÓÊÜÁ½¸ö²ÎÊý£ºÒ»¸öÊÇURL£¨arg0 £©£¬Ò»¸öÊÇJSON¶ÔÏó£¨arg1 £©£¬È»ºó·µ»ØÒ»¸ö×Ö·û´®¡£ ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓÃjson ·½·¨µÄʾÀý´úÂ룺
var http1 = new okHttp();
var jsonData = {
'a': '1',
'b': '2'
};
var response = http1.json('http://www.baidu.com', jsonData);
printl(response);
´úÂë˵Ã÷- ´´½¨okHttpʵÀý£ºÊ¹ÓÃ
new okHttp() ´´½¨Ò»¸öеÄHTTPʵÀý¡£ - ×¼±¸JSONÊý¾Ý£º¶¨ÒåÒ»¸öJavaScript¶ÔÏ󣬲¢½«Æä×÷ΪJSONÊý¾Ý·¢ËÍ¡£
- ·¢ËÍÇëÇ󣺵÷ÓÃ
http1.json(url, jsonData) ·¢ËÍHTTPÇëÇó£¬URLºÍÒª·¢Ë͵ÄJSONÊý¾Ý¡£ - ´òÓ¡ÏìÓ¦£º½«ÏìÓ¦½á¹ûͨ¹ý
printl Êä³ö¡£
×¢ÒâÊÂÏî- È·±£URLÓÐЧÇÒÄܹ»½ÓÊÜJSONÊý¾Ý¡£
- ÑéÖ¤JSONÊý¾Ý¸ñʽÊÇ·ñÕýÈ·£¬È·±£¿ÉÒÔ±»·þÎñÆ÷ÕýÈ·½âÎö¡£
µÚÊ®¸ö£º
POST ÇëÇóµÄº¯Êý˵Ã÷£¬°üº¬·½·¨µÄ²ÎÊýºÍʾÀý´úÂë¡£ÒÔÏÂÊÇÏêϸµÄ½éÉÜ£º º¯Êý˵Ã÷º¯ÊýÃû³Æ: post ·µ»ØÖµ: String ²ÎÊý: String arg0 : ÕâÊÇ POST ÇëÇóµÄ URL¡£map arg1 : ÕâÊÇÒ»¸ö°üº¬Òª·¢Ë굀 POST Êý¾ÝµÄ¼üÖµ¶Ô¼¯ºÏ¡£
ʾÀý´úÂë¼ÙÉèÎÒÃÇҪʹÓÃ´Ë post ·½·¨·¢ËÍ POST ÇëÇó£¬ÒÔÏÂÊÇ´úÂëʾÀý£º
var http = new OkHttp();
var postData = {
"name": "ÕÅÈý",
"age": "25"
};
var url = "http://example.com/api";
var response = http.post(url, postData);
printl(response);
´úÂë˵Ã÷- ´´½¨ OkHttp ʵÀý£ºÊ¹ÓÃ
new OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£ - ×¼±¸ POST Êý¾Ý£º¹¹ÔìÒ»¸ö¼òµ¥µÄ¶ÔÏóÀ´´æ´¢Òª·¢Ë͵ÄÊý¾Ý¡£
- ·¢ËÍÇëÇ󣺵÷ÓÃ
http.post(url, postData) ·½·¨·¢ËÍ POST ÇëÇ󣬲¢´«ÈëÄ¿±ê URL ºÍ POST Êý¾Ý¡£ - Êä³öÏìÓ¦£ºÊ¹ÓÃ
printl() º¯Êý½«·þÎñÆ÷µÄÏìÓ¦½á¹û´òÓ¡³öÀ´¡£
×¢ÒâÊÂÏî- È·±£ URL ÓÐЧÐÔ£ºÇëÈ·±£ÄúÌṩµÄ URL ÊÇÓÐЧÇÒ¿É·ÃÎʵġ£
- Êý¾Ý¸ñʽ£ºÈ·ÈÏ POST Êý¾ÝµÄ¸ñʽ·ûºÏ·þÎñÆ÷µÄÒªÇó£¬ÒÔ±ãÄÜÕýÈ·½ÓÊÕ¡£
ͨ¹ýÒÔÉϲ½Ö裬Äú¿ÉÒÔʹÓà post ·½·¨·¢ËÍ POST ÇëÇó²¢´¦Àí·þÎñÆ÷µÄÏìÓ¦¡£Èç¹ûÓÐÌض¨ÐèÇ󣬿ÉÒÔ¸ù¾Ýʵ¼ÊÇé¿ö½øÐе÷Õû¡£ µÚʮһ¸ö£º
·¢ËÍ POST ±íµ¥ÇëÇóµÄº¯Êý˵Ã÷£¬°üÀ¨·½·¨µÄ²ÎÊýºÍʾÀý´úÂë¡£ÒÔÏÂÊÇÏêϸµÄ½éÉÜ£º º¯Êý˵Ã÷º¯ÊýÃû³Æ: postForm ·µ»ØÖµ: String ²ÎÊý: String arg0 : ÕâÊÇ POST ÇëÇóµÄ URL¡£String arg1 : ÕâÊÇÒª·¢Ë굀 POST Êý¾Ý£¨Í¨³£ÒÔ²éѯ×Ö·û´®¸ñʽ´«µÝ£©¡£
ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓà postForm ·½·¨µÄʾÀý´úÂ룺
var http = new OkHttp();
var postData = "name=ÕÅÈý&age=25";
var url = "http://example.com/api";
var response = http.postForm(url, postData);
printl(response);
´úÂë˵Ã÷- ´´½¨ OkHttp ʵÀý£ºÊ¹ÓÃ
new OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£ - ×¼±¸ POST Êý¾Ý£ºÒÔ²éѯ×Ö·û´®µÄ¸ñʽ׼±¸Òª·¢Ë͵ÄÊý¾Ý£¬Èç
name=ÕÅÈý&age=25 ¡£ - ·¢ËÍÇëÇ󣺵÷ÓÃ
http.postForm(url, postData) ·½·¨·¢ËÍ POST ÇëÇ󣬲¢´«ÈëÄ¿±ê URL ºÍ POST Êý¾Ý¡£ - Êä³öÏìÓ¦£ºÊ¹ÓÃ
printl() ½«·þÎñÆ÷µÄÏìÓ¦½á¹û´òÓ¡³öÀ´¡£
×¢ÒâÊÂÏî- È·±£ URL ÓÐЧÐÔ£ºÇëÈ·ÈÏÄúÌṩµÄ URL ÊÇÓÐЧÇÒ¿É·ÃÎʵġ£
- Êý¾Ý¸ñʽ£ºÈ·±£ POST Êý¾ÝµÄ¸ñʽ·ûºÏ·þÎñÆ÷µÄÒªÇó£¬Í¨³£Îª URL ±àÂë¸ñʽ¡£
µÚÊ®¶þ¸ö£º
ÉèÖÃ×Ö·û¼¯µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º º¯Êý˵Ã÷º¯ÊýÃû³Æ: setCharSet ·µ»ØÖµ: void ²ÎÊý: String arg0 : ÕâÊÇÒªÉèÖõÄ×Ö·û¼¯£¬¿ÉÒÔÊÇ UTF-8 »ò GBK ¡£´«Èë null ½«²»½øÐбàÂ룬ĬÈÏΪ UTF-8 ¡£
ʾÀý´úÂëÏÂÃæÊÇÈçºÎʹÓà setCharSet ·½·¨½øÐÐ×Ö·û¼¯ÉèÖõÄʾÀý´úÂ룺
var http = new OkHttp();
http.setCharSet("UTF-8");
http.setCharSet("GBK");
http.setCharSet(null);
´úÂë˵Ã÷- ´´½¨ OkHttp ʵÀý: ʹÓÃ
new OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£ - ÉèÖÃ×Ö·û¼¯:
- ʹÓÃ
http.setCharSet("UTF-8") ÉèÖÃ×Ö·û¼¯Îª UTF-8¡£ - ʹÓÃ
http.setCharSet("GBK") ÉèÖÃ×Ö·û¼¯Îª GBK¡£ - ʹÓÃ
http.setCharSet(null) Èç¹û²»Ïë½øÐÐÌØÊâ±àÂë¡£
×¢ÒâÊÂÏî- ºÏÀíÑ¡Ôñ×Ö·û¼¯: ¸ù¾Ýʵ¼ÊÐèÇóÑ¡ÔñºÏÊʵÄ×Ö·û¼¯À´È·±£Êý¾ÝÕýÈ·´«ÊäºÍ´æ´¢¡£
- ĬÈÏÖµ: Èç¹ûδÉèÖÃ×Ö·û¼¯£¬Ä¬ÈÏΪ UTF-8¡£
ͨ¹ýÒÔÉϲ½Ö裬Äú¿ÉÒÔʹÓà setCharSet ·½·¨Áé»îÉèÖÃ×Ö·û¼¯£¬ÒÔÂú×㲻ͬµÄ±àÂëÐèÇ󡣸ù¾ÝÄúµÄ¾ßÌ峡¾°½øÐе÷Õû¡£ µÚÊ®Èý¸ö£º
Ìí¼Ó Cookie µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º º¯Êý˵Ã÷º¯ÊýÃû³Æ: setCookie ·µ»ØÖµ: void ²ÎÊý: String arg0 : ²ÎÊýÃû³Æ£¨Cookie µÄÃû³Æ£©¡£String arg1 : ²ÎÊýÖµ£¨Cookie µÄÖµ£©¡£
ʾÀý´úÂëÏÂÃæÊÇÈçºÎʹÓà setCookie ·½·¨Ìí¼Ó Cookie µÄʾÀý´úÂ룺
var http = new OkHttp();
var cookieName = "session_id";
var cookieValue = "abc123xyz";
http.setCookie(cookieName, cookieValue);
printl("Cookie ÒÑÉèÖÃ: " + cookieName + "=" + cookieValue);
´úÂë˵Ã÷- ´´½¨ OkHttp ʵÀý: ʹÓÃ
new OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£ - ÉèÖà Cookie:
- ͨ¹ýµ÷ÓÃ
http.setCookie(cookieName, cookieValue) ½«Ö¸¶¨µÄ Cookie Ãû³ÆºÍÖµÌí¼Óµ½ÇëÇóÖС£
×¢ÒâÊÂÏî- È·±£ÓÐЧÐÔ: È·±£ÉèÖÃµÄ Cookie Ãû³ÆºÍÖµ·ûºÏ·þÎñÆ÷µÄÔ¤ÆÚ¡£
- Cookie ×÷ÓÃ: Ìí¼ÓµÄ Cookie ½«»áÔÚºóÐøµÄÇëÇóÖз¢Ë͸ø·þÎñÆ÷£¬ÒÔ±ãÓڻỰ¹ÜÀí»òÓû§Éí·ÝÑéÖ¤¡£
µÚÊ®Ëĸö£º
¹ØÓÚÉèÖà HTTP Í·µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º º¯Êý˵Ã÷º¯ÊýÃû³Æ: setHeader ·µ»ØÖµ: void ²ÎÊý: String arg0 : ²ÎÊýÃû³Æ£¨¼´ HTTP Í·µÄÃû³Æ£©¡£String arg1 : ²ÎÊýÖµ£¨¼´ HTTP Í·¶ÔÓ¦µÄÖµ£©¡£
ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓà setHeader ·½·¨ÉèÖà HTTP Í·µÄʾÀý´úÂ룺
let http1 = new okHttp();
http1.setHeader("token", "12345667");
http1.setHeader("User-Agent", "Mozilla/5.0");
´úÂë˵Ã÷- ´´½¨ OkHttp ʵÀý: ʹÓÃ
new okHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£ - ÉèÖà HTTP Í·:
- ͨ¹ýµ÷ÓÃ
http1.setHeader("token", "12345667") ÉèÖÃÃûΪ token µÄ HTTP Í·¡£ - ͬÑù¿ÉÒÔÉèÖÃÆäËû HTTP Í·£¬±ÈÈç
User-Agent ¡£
×¢ÒâÊÂÏî- È·±£ÕýÈ·ÐÔ: È·±£ÉèÖõÄÍ·ÐÅÏ¢·ûºÏ·þÎñÆ÷µÄÒªÇ󣬱ÜÃâÒò´íÎóµÄÍ·ÐÅÏ¢µ¼ÖÂÇëÇóʧ°Ü¡£
- ¶à¸öÍ·ÐÅÏ¢: ¿ÉÒÔ¶à´Îµ÷ÓÃ
setHeader ·½·¨ÉèÖöà¸ö HTTP Í·¡£
µÚÊ®Îå¸ö£º
¹ØÓÚÉèÖÃÊý¾Ý·¢Ëͳ¬Ê±Ê±¼äµÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º º¯Êý˵Ã÷º¯ÊýÃû³Æ: setTimeOut ·µ»ØÖµ: void ²ÎÊý: int arg0 : ·¢Ëͳ¬Ê±Ê±¼ä£¨µ¥Î»£ºÃ룩¡£int arg1 : ¶ÁÈ¡³¬Ê±Ê±¼ä£¨µ¥Î»£ºÃ룩¡£int arg2 : Á¬½Ó³¬Ê±Ê±¼ä£¨µ¥Î»£ºÃ룩¡£
ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓà setTimeOut ·½·¨ÉèÖó¬Ê±Ê±¼äµÄʾÀý´úÂ룺
let http1 = new okHttp();
http1.setTimeOut(30, 15, 10);
´úÂë˵Ã÷- ´´½¨ OkHttp ʵÀý: ʹÓÃ
new okHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£ - ÉèÖó¬Ê±Ê±¼ä:
- µ÷ÓÃ
http1.setTimeOut(30, 15, 10) ÉèÖ÷¢Ëͳ¬Ê±Ê±¼äΪ 30 Ã룬¶ÁÈ¡³¬Ê±Ê±¼äΪ 15 Ã룬Á¬½Ó³¬Ê±Ê±¼äΪ 10 Ãë¡£
×¢ÒâÊÂÏî- ºÏÀíÉèÖó¬Ê±Ê±¼ä: ¸ù¾ÝÍøÂç»·¾³ºÍÇëÇóµÄÐèÇ󣬺ÏÀíÉèÖó¬Ê±Ê±¼ä£¬ÒÔ·ÀÖ¹ÇëÇóÒò³¬Ê±¶øʧ°Ü¡£
- µ¥Î»: ËùÓг¬Ê±Ê±¼äµÄµ¥Î»¾ùΪÃ룬ϵͳ»á×Ô¶¯×ª»»ÎªÊʵ±µÄºÁÃëÖµ½øÐд¦Àí¡£
µÚÊ®Áù¸ö£º
¹ØÓÚÎļþÉÏ´«µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º º¯Êý˵Ã÷º¯ÊýÃû³Æ: uploadFile ·µ»ØÖµ: String ²ÎÊý: String arg0 : Ä¿±ê URL£¨¼´ÎļþÉÏ´«µÄµØÖ·£©¡£String arg1 : ²ÎÊýÃû³Æ£¨ÓÃÓÚÉÏ´«µÄ²ÎÊýÃû£¬Í¨³£ÊÇÎļþ×ֶεÄÃû³Æ£©¡£String arg2 : Îļþ·¾¶£¨¼´ÒªÉÏ´«µÄÎļþÔÚ±¾µØµÄ·¾¶£©¡£map arg3 : ÆäËû±íµ¥Êý¾Ý£¨ÒÔ¼üÖµ¶ÔÐÎʽ´«µÝµÄ¶îÍâÊý¾Ý£©¡£
ʾÀý´úÂëÒÔÏÂÊÇÈçºÎʹÓà uploadFile ·½·¨ÉÏ´«ÎļþµÄʾÀý´úÂ룺
let http = new okHttp();
let url = "http://example.com/upload";
let paramName = "file";
let filePath = "/path/to/your/file.txt";
let additionalData = {
"description": "This is a test file.",
"userId": "12345"
};
let response = http.uploadFile(url, paramName, filePath, additionalData);
printl(response);
´úÂë˵Ã÷- ´´½¨ OkHttp ʵÀý: ʹÓÃ
new okHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£ - ×¼±¸ÉÏ´«ÎļþµÄ²ÎÊý:
- Ö¸¶¨ÎļþÉÏ´«µÄ URL¡¢²ÎÊýÃû³ÆÒÔ¼°ÎļþµÄ±¾µØ·¾¶¡£
- ×¼±¸ÆäËû±íµ¥Êý¾Ý: ´´½¨Ò»¸ö¶ÔÏóÀ´´æ´¢¸½¼ÓµÄ±íµ¥Êý¾Ý¡£
- Ö´ÐÐÎļþÉÏ´«: µ÷ÓÃ
uploadFile ·½·¨½øÐÐÎļþÉÏ´«£¬²¢´«ÈëÏàÓ¦µÄ²ÎÊý¡£ - Êä³öÏìÓ¦: ʹÓÃ
printl() ´òÓ¡·þÎñÆ÷µÄÏìÓ¦½á¹û¡£
×¢ÒâÊÂÏî- È·±£ URL ÓÐЧÐÔ: ÇëÈ·ÈÏËùÌṩµÄ URL ÊÇÓÐЧÇÒ¿É·ÃÎʵġ£
- Îļþ·¾¶: È·±£ËùÖ¸¶¨µÄÎļþ·¾¶ÊÇÕýÈ·µÄ£¬²¢ÇÒÓжÁȡȨÏÞ¡£
- Êý¾Ý¸ñʽ: ÉÏ´«µÄÎļþºÍÆäËû±íµ¥Êý¾ÝÓ¦·ûºÏ·þÎñÆ÷µÄÒªÇó£¬ÒÔ±ÜÃâÉÏ´«Ê§°Ü¡£
ͨ¹ýÒÔÉϲ½Ö裬Äú¿ÉÒÔʹÓà uploadFile ·½·¨ÇáËÉÉÏ´«Îļþ²¢´¦Àí·þÎñÆ÷µÄÏìÓ¦¡£ÈçÐè¸ù¾Ýʵ¼ÊÇé¿ö½øÐе÷Õû£¬È·±£´úÂëÂß¼·ûºÏÐèÇó¡£
var okhttp1 = new okHttp();
var cookie = okhttp1.getCookie();
printl(cookie);
var okhttp1 = new okHttp();
okhttp1.setHeader('token', 'ÄúµÄtokenÖµ');
okhttp1.setHeader('user-agent', 'ÄúµÄUser-Agent');
var cookie = okhttp1.getCookie();
okhttp1.setCookie('cookie_name', cookie);
printl('CookieÒÑÉèÖÃ: cookie_name=' + cookie);
var http1 = new okHttp();
http1.setHeader("User-Agent", "Mozilla/5.0");
var res = http1.get("https://v.kuaishou.com/nQtui");
var responseHeaders = http1.getResponseHeaders();
printl(responseHeaders);
let http1 = new okHttp();
http1.setHeader("token", "12345667");
http1.setHeader("User-Agent", "Mozilla/5.0");
var http1 = new okHttp();
http1.setHeader("token", "12345667");
http1.setHeader("User-Agent", "Mozilla/5.0");
http1.clearHeader();
http1.setHeader("New-Header", "newValue");
var okhttp1 = new okHttp();
okhttp1.setHeader('token', 'ÄúµÄToken');
var fileUrl = 'http://example.com/path/to/your/file.zip';
var localPath = '/sdcard/Download/file.zip';
var downloadResponse = okhttp1.downloadFile(fileUrl, localPath);
printl(downloadResponse);
var fileUrl = "http://example.com/path/to/your/file.zip";
var localPath = "/sdcard/Download/file.zip";
function progressCallback(percentage) {
printl("ÏÂÔؽø¶È: " + percentage + "%");
}
var response = downloadFileProgress(fileUrl, localPath, progressCallback);
printl(response);
var url = "http://kg.lbzyw.top/api/banks/collect";
var response = http.get(url);
printl(response);
var http1 = new okHttp();
var jsonData = {
'a': '1',
'b': '2'
};
var response = http1.json('http://www.baidu.com', jsonData);
printl(response);
var http = new OkHttp();
var postData = {
"name": "ÕÅÈý",
"age": "25"
};
var url = "http://example.com/api";
var response = http.post(url, postData);
printl(response);
var http = new OkHttp();
var postData = "name=ÕÅÈý&age=25";
var url = "http://example.com/api";
var response = http.postForm(url, postData);
printl(response);
var http = new OkHttp();
http.setCharSet("UTF-8");
http.setCharSet("GBK");
http.setCharSet(null);
let http = new okHttp();
let url = "http://example.com/upload";
let paramName = "file";
let filePath = "/path/to/your/file.txt";
let additionalData = {
"description": "This is a test file.",
"userId": "12345"
};
let response = http.uploadFile(url, paramName, filePath, additionalData);
printl(response);
|