首页 > 其他分享 >RestKit学习2:使用RestKit发送和接受请求

RestKit学习2:使用RestKit发送和接受请求

时间:2023-06-22 11:33:39浏览次数:29  
标签:RKClient resource 请求 NSLog request 发送 RestKit response

首先请看本系列的上一篇文章:RestKit学习1:引用

RestKit项目 ,这篇文章是RestKit的简单使用介绍。

参考:https://github.com/RestKit/RestKit/wiki/Tutorial-%3A-Introduction-to-RestKit

RestKit 支持网络层的请求,网络层的功能包括:建立和调度网络请求,以及请求结果响应处理。

一般来说,我们用RKClient来处理这些请求。RKClient 是一个可以连接到指定服务器的客户端对象。它初始化基本URL,HTTP头,验证信息。它支持两种验证:HTTP Authentication 和 OAuth。你可以初始化很多实例,当然也有一个全局的单件实例,全局的单件实例常常在应用的applicationDidFinishLaunching:withOptions方法中出现:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
{
    [RKClient clientWithBaseURLString:@"http://www.in4s.cn:8080/Inpads/json"];
    NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
    return YES;
}

发送请求以及接受请求代码:

#import <RestKit/RestKit.h>

  // Here we declare that we implement the RKRequestDelegate protocol
  // Check out RestKit/Network/RKRequest.h for additional delegate methods
  // that are available.
  @interface RKRequestExamples : NSObject <RKRequestDelegate> {
  }

  @end

  @implementation RKRequestExamples

  - (void)sendRequests {
    // Perform a simple HTTP GET and call me back with the results
    [ [RKClient sharedClient] get:@"/foo.xml" delegate:self];

    // Send a POST to a remote resource. The dictionary will be transparently
    // converted into a URL encoded representation and sent along as the request body
    NSDictionary* params = [NSDictionary dictionaryWithObject:@"RestKit" forKey:@"Sender"];
    [ [RKClient sharedClient] post:@"/other.json" params:params delegate:self];

    // DELETE a remote resource from the server
    [ [RKClient sharedClient] delete:@"/missing_resource.txt" delegate:self];
  }

  - (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
    if ([request isGET]) {
      // Handling GET /foo.xml

      if ([response isOK]) {
        // Success! Let's take a look at the data
        NSLog(@"Retrieved XML: %@", [response bodyAsString]);
      }

    } else if ([request isPOST]) {
      
      // Handling POST /other.json 
      if ([response isJSON]) {
        NSLog(@"Got a JSON response back from our POST!");
      }

    } else if ([request isDELETE]) {

      // Handling DELETE /missing_resource.txt
      if ([response isNotFound]) {
        NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
      }
    }
  }

  @end

 

执行程序,我们可以通过NSLog输入的信息看到接受到的内容。

 

参考资料:

http://mobile.tutsplus.com/tutorials/iphone/restkit_ios-sdk/

标签:RKClient,resource,请求,NSLog,request,发送,RestKit,response
From: https://blog.51cto.com/u_15588078/6534482

相关文章

  • 请求转发和重定向
    请求转发(Forward)和重定向(Redirect)是Web开发中两种常见的页面跳转方式,它们有不同的实现机制和应用场景。请求转发(Forward):请求转发是在服务器内部完成的,浏览器并不知道页面发生了转发。在服务器端,通过调用HttpServletRequest对象的forward()方法将请求转发到另一个资源(如Servle......
  • Android Kotlin Retrofit MVP网络请求封装(四)
    依赖implementation'com.squareup.retrofit2:retrofit:2.9.0'implementation'com.google.code.gson:gson:2.8.8'implementation'com.squareup.okhttp3:okhttp:4.9.1'implementation'com.squareup.retrofit2:retrof......
  • python请求有关ja3指纹问题
    遇见一个网站采集,无论怎样都返回空数据(实际上是有数据的),但是抓包下来又确实是那样的,请教了一些人推测是指纹验证,拜读了网上其他大佬的博客文章后实验了一下,发现确实是这个问题!第一次知道tcp还有这个东西,让我大受震撼,值此搬运一下。参考链接及来源:Python爬虫进阶必备|JA3指......
  • java+rest方式写一个邮件发送接口
     1<!--发邮件-->2<dependency>3<groupId>org.springframework.boot</groupId>4<artifactId>spring-boot-starter-mail</artifactId>5</dependency>67<dep......
  • go语言解析HTTP包生成代码发送HTTP数据包
    输入是从fiddler捕获的HTTP数据包GEThttps://bbs.kanxue.com/HTTP/1.1Host:bbs.kanxue.comConnection:keep-aliveUpgrade-Insecure-Requests:1User-Agent:Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/114.0.0.0Safari......
  • 使用RocketMQ组件对请求做削峰处理
    内容rocketMQ基本介绍使用MQ,将购票流程一分为二。目前系统的吞吐量低,用户从购买车票到拿到票花费的时间较长。增加排队购票功能。排队提示loading。购票时序图目前的时序图,用户发送购票请求,服务端校验验证码,拿令牌,拿锁,然后选座购票,结束流程才会返回。服务器执行时间太长。......
  • 9. SpringMVC处理ajax请求
    9.1、@RequestBody@RequestBody可以获取请求体信息,使用@RequestBody注解标识控制器方法的形参,当前请求的请求体就会为当前注解所标识的形参赋值<!--此时必须使用post请求方式,因为get请求没有请求体--><formth:action="@{/test/RequestBody}"method="post"> 用户名:<inp......
  • C#模拟QQ发送消息
    非QQ协议发送消息, 而是使用桌面qq, 然后程序模拟windows按键, 使用qq发送消息. 可以改为Cosole平台,OWIN接收信息, 给个人或群发送聊天信息. 程序流程:1.聊天内容复制到剪切板2.遍历QQ窗口找到指定窗口3.发送激活窗口命令,模拟发送Ctrl+V,Ctrl+回车usingSystem;......
  • Java类属性第二个字母大写问题,请求参数设置不上,返回参数小写
     其实这个问题几年前就遇到过,也解决了,但是最近又看到项目中有人这么用,就想起来了,写在这里,给自己也给大家提个醒。在Java中,如果类的某个属性第二个字母是大写,比如:nToken,这样的属性一定要自己手动生成getter和setter方法。如果使用lombok的@Data注解,它默认生成的getter和setter......
  • fastadmin 的Http类 请求外部接口携带 Authorization:Bearer token 参数问题
    背景:最近在对接某个系统的支付接口时,接口请求时要求携带token,在请求头header中添加Authorization:Bearer。我使用的框架tp5搭建的fastadmin,里面封装了Http类 出现问题:写法出错,虽然带了参数,但是对方接受不到参数,接口请求验证失败  解决方法:正确的写法代码如下:$info=Ht......