🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~[api] 请求示例 <<< APICloud示例 &lt;script type="text/javascript" src="http://static.12xue.com/openapi/apicloud/1.0/12xue.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; apiready = function(){ // 初始化12xueApi var appId = '12xue_tch'; var appSecret = '7e4602f8a********************e24b42b78f'; var access_token = ''; // 线上环境可以省略baseUrl参数 var baseUrl = 'http://api.12xuedev.com/'; var client = new _12xue(appId, AppSecret, baseUrl); // 设置个性化的header client.setHeaders('Source', 'teacher_android'); client.setHeaders('Version', '1.0.0'); client.setHeaders('Token', access_token); // 生成接口实例并发起请求 client.factory('user/login') //【可选】设置上传的文件,与apicloud中ajax请求时data中的files对应 .setFiles('file', 'fs://aaa.jpg') //【可选】设置上传的文件流,与apicloud中ajax请求时data中的stream对应 .setStream('fs:://aaa.jpg') //【可选】设置上传的文件流,与apicloud中ajax请求时data中的body对应 // 注意:如果body为json字符串时需设置application/json类型的Content-Type头 .setBody('{username:\'zhangsan\'}') // 具体提交方式,支持post、get、put、delete .post({ account: 'kfadmin', password: '******', client_id: '12xue_tch', client_secret: '7e4602f8a5**************24b42b78f' }, function(ret, err){ // 如果正常返回ret,否则ret为null if (ret) { console.log( JSON.stringify( ret ) ); } else { console.log( JSON.stringify( err ) ); } }); } &lt;/script&gt; <<< php示例 $ch = curl_init(); $url = 'http://api.12xuedev.com/user/info'; $header = array( 'Token: 您自己的Token', 'Source: 您的应用来源', /* student_android student_ios teacher_ios teacher_android headermaster_android headermaster_ios parent_ios parent_android web teacher_pc */ 'Version: 当前应用版本' ); // 参数信息 $data = [ 'uid'=>123 ]; // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); // 添加参数 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行HTTP请求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); var_dump(json_decode($res)); <<< java示例 String httpUrl = "http://api.12xuedev.com/user/info"; String httpArg = "{ "uid":123 }"; String jsonResult = request(httpUrl, httpArg); System.out.println(jsonResult); /** * @param urlAll * :请求接口 * @param httpArg * :参数 * @return 返回结果 */ public static String request(String httpUrl, String httpArg) { BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("POST"); // 填入您自己的Token到HTTP header connection.setRequestProperty("Token", "您自己的Token"); // 填入您的应用来源到HTTP header connection.setRequestProperty("Source", "您的应用来源"); // 填入当前应用版本到HTTP header connection.setRequestProperty("Version", "当前应用版本"); connection.setDoOutput(true); connection.getOutputStream().write(httpArg.getBytes("UTF-8")); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } <<< ObjectC示例 NSString *httpUrl = @"http://api.12xuedev.com/user/info"; NSString *httpArg = @"{ "uid":123 }"; [self request: httpUrl withHttpArg: httpArg]; -(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg { NSURL *url = [NSURL URLWithString: httpUrl]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; [request setHTTPMethod: @"POST"]; [request addValue: @"您自己的Token" forHTTPHeaderField: @"Token"]; [request addValue: @"您的应用来源" forHTTPHeaderField: @"Source"]; [request addValue: @"当前应用版本" forHTTPHeaderField: @"Version"]; NSData *data = [HttpArg dataUsingEncoding: NSUTF8StringEncoding]; [request setHTTPBody: data]; [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){ if (error) { NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code); } else { NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode]; NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"HttpResponseCode:%ld", responseCode); NSLog(@"HttpResponseBody %@",responseString); } }]; } ~~~