使用带有AFHHTPRequestoperation的java客户端/ iOS客户端

时间:2014-04-07 21:04:44

标签: java ios web-services client java-server

尝试在具有iOS客户端的Java服务器中实现虚拟Web服务

java服务正在侦听本地端口,然后启动正在返回的响应线程

 public void run() {
    try {
        InputStream input  = clientSocket.getInputStream();
        OutputStream output = clientSocket.getOutputStream();
        long time = System.currentTimeMillis();
        output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " + this.serverText + " - Hello Phil!" +time +"").getBytes());
        output.close();
        System.out.println(input);
        System.out.println(output);
        input.close();
        System.out.println("Request processed: " + time);
    } catch (IOException e) {
        //report exception somewhere
        e.printStackTrace();
    }

iOS客户端正在发送请求

NSLog(@"testing...");
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:9000"]];

// Initialize Request Operation
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:URL]];

// Configure Request Operation
[requestOperation setResponseSerializer:[AFJSONResponseSerializer serializer]];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSString *response;


    response = [responseObject responseString];

    NSLog(@" %@ ", response);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {


    NSLog(@"failed");




}];

// Start Request Operation
[requestOperation start];

NSLog(@"connecting...");

每次iOS返回失败 - 我可以从浏览器点击它,我可以看到我的java服务器发回数据 -

我回复数据错了吗?我应该使用什么格式?似乎找不到任何JSON / XML SOAP / etc

的简单示例

提前致谢

菲尔

1 个答案:

答案 0 :(得分:0)

您可以记录错误以找出问题所在。在代码中替换此部分:

...
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
    NSLog(@"error: %@", error);
}
...