0

Windows サーバーとデータを共有する必要があるアプリに取り組んでいます。私は Web でいくつかの例を見て、以下にリストされている実装を考え出しました。iOS コードと Web サービスは独立して正常に動作しますが、それらを接続する方法がわかりません。私が見たすべての例は、Web サービスの URL を示していません (少なくとも、何が起こっているのかを理解できる方法ではありません)。ServerURL の値を見て、どうあるべきか提案してください。エントリ ポイント「doSomething」の名前をどこかに含めるべきだと思いますが、構文がどのように見えるべきかわかりません。私は試してみhttp://texas/WebSite3/Service.asmx/doSomethingましhttp://texas/WebSite3/Service.asmx?op=doSomethingたが、どちらもうまくいかないようです。他に問題がある場合はお知らせください。

ローカル システムで動作するようになったら、Texas を完全なドメイン名に置き換えることができると思います。

iOSコードは次のとおりです。

-(void)checkWithServer {
// Create the POST request payload.
    NSDictionary *tmp = [[NSDictionary alloc]initWithObjectsAndKeys:
                     @"test@gmail.com", @"email",
                     @"John Doe", @"name",
                     nil];
    NSError *error;
    NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];

    NSString *serverURL = @"http://texas/WebSite3/Service.asmx";

// Create the POST request to the server.
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [postdata length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postdata];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [conn start];
}

これが通信する Web サービスは次のとおりです。

    [WebService(Namespace = "http://texas/WebSite3")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {
        public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string doSomething(string msg)
    {
        byte[] buffer = GetBytes(msg);
        XmlReader reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(buffer, System.Xml.XmlDictionaryReaderQuotas.Max);
        XElement root = XElement.Load(reader);

        string result = "{\"result\":\"none\"}";

        return result;
    }
4

2 に答える 2

1

Axeva の助けを借りて、これが私が思いついたものです。

iOS コード:

-(void)checkWithServer {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    NSURL *postURL = [NSURL URLWithString: @"http://texas/WebSite3/Service.asmx/doSomething"];
    NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"test@gmail.com", @"email",
                              @"John Doe", @"name",
                              nil];

    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: postURL
                                                           cachePolicy: NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval: 60.0];

    [request setHTTPMethod: @"POST"];
    [request setValue: @"application/json" forHTTPHeaderField: @"Accept"];
    [request setValue: @"application/json; charset=utf-8" forHTTPHeaderField: @"content-type"];
    [request setHTTPBody: jsonData];

    [NSURLConnection sendAsynchronousRequest: request
                                       queue: queue
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
                               if (error || !data) {
                                   // Handle the error
                               } else {
                                   // Handle the success
                               }
                           }
     ];
}

Web サーバー コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Xml;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://texas/WebSite3")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment   the following line. 
[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string doSomething(string email, string name)
    {
        return "{\"name\": \"" + name + "\", \"email\": \"" + email + "\"}";
    }
}

どうやら、WebサーバーはJSONを分離して変数nameとemailを割り当てるのに十分スマートです。

于 2013-04-09T21:01:16.290 に答える