AFNetworking3.0.4的使用

在新版本中没有在使用:
                AFURLConnectionOperation
                AFHTTPRequestOperation
                AFHTTPRequestOperationManager

  1. // 初始化Manager
  2. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  3. // 不加上这句话,会报“Request failed: unacceptable content-type: text/plain”错误,因为我们要获取text/plain类型数据
  4. //sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
  5. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  6. // post请求
  7. [manager POST:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?" parameters:dic constructingBodyWithBlock:^(id _Nonnull formData) {
  8. // 拼接data到请求体,这个block的参数是遵守AFMultipartFormData协议的。
  9. } progress:^(NSProgress * _Nonnull uploadProgress) {
  10. // 这里可以获取到目前的数据请求的进度
  11. } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  12. // 请求成功,解析数据
  13. NSLog(@"%@", responseObject);
  14. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
  15. NSLog(@"%@", dic);
  16. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  17. // 请求失败
  18. NSLog(@"%@", [error localizedDescription]);
  19. }];


  1. // Get请求
  2. [manager GET:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
  3. // 这里可以获取到目前的数据请求的进度
  4. } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  5. // 请求成功,解析数据
  6. NSLog(@"%@", responseObject);
  7. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
  8.  
  9. NSLog(@"%@", dic);
  10. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  11. // 请求失败
  12. NSLog(@"%@", [error localizedDescription]);
  13. }];


  1. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

  注:     // 停止所有操作
          [self.manager.operationQueue  cancelAllOperations];

       // 取消所有任务
  //  [self.manager.tasks  makeObjectsPerformSelector:@selector(cancel)];
      [self.manager  invalidateSessionCancelingTasks:YES];

Feign是一个HTTP客户端工具,用于简化HTTP API客户端的开发。它是一个声明式的Web服务客户端,使用Spring Cloud Feign可以轻松地实现微服务之间的调用。 在Spring Boot 3.0.4中使用Feign的步骤如下: 1. 添加Feign依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 创建Feign客户端接口 创建一个接口,使用@FeignClient注解来定义Feign客户端,例如: ``` @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/example") String getExample(); } ``` 在上面的例子中,@FeignClient注解中的name属性指定了要调用的服务的名称,getExample()方法中的@GetMapping注解指定了要调用的服务端点。 3. 注入并使用Feign客户端 在需要使用Feign客户端的类中,使用@Autowired注解来注入ExampleServiceClient接口,例如: ``` @RestController public class ExampleController { @Autowired private ExampleServiceClient exampleServiceClient; @GetMapping("/example") public String getExample() { return exampleServiceClient.getExample(); } } ``` 在上面的例子中,ExampleController类中的getExample()方法中调用了ExampleServiceClient接口的getExample()方法来获取服务端点的响应。 4. 启用Feign客户端 在Spring Boot应用程序的主类中,使用@EnableFeignClients注解来启用Feign客户端,例如: ``` @SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 以上就是使用Feign的基本步骤,需要注意的是,Feign需要与Ribbon配合使用才能实现负载均衡功能,因此在使用Feign时需要保证Ribbon也已经正确配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值