iPhone上的JSON POST请求(使用HTTP)问题

时间:2011-04-11 13:23:26

标签: objective-c nsmutableurlrequest

我对asp .net mvc Web服务的请求有问题。我刚才读了一个帖子,它可以找出服务器想要的内容类型是什么等等。编译时没有错误但是当我做实际请求时没有发生任何事情并且在服务器的日志中它只说(null)(null)。执行GET请求和查看列表中的所有对象都没有问题。有谁能帮助我解决这个烦人的错误?这是代码:

//----------------GET request to webservice works fine----------------------------------------
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
[request setHTTPMethod: @"GET"];
NSData *response = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *stringResponse = [[NSString alloc] initWithData: response encoding: NSUTF8StringEncoding];
//NSLog(@"stringResponse is %@", stringResponse);
//--------------------------------------------------------------------------------------------
NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSString *output=
[NSString stringWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
id theObject=   [output JSONValue];
NSLog(@"TWITTER: %@",theObject);
*/
//--------------------------------------------------------------------------------------------

NSURL *url = [NSURL URLWithString:@"http://errorreport.abou.se/Errors/1.0"];



//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\"}";
//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\",\"Address\":\"smedjegatan\",\"StreetNumber\":\"34\",\"Feedback\":\"True\",\"FeedbackWay\":\"Telefon\"}";

NSMutableDictionary* jsonObject = [NSMutableDictionary dictionary];
//NSMutableDictionary* metadata = [NSMutableDictionary dictionary];
//[metadata setObject:@"NewLoc" forKey:@"Uri"];
//[metadata setObject:@"Location.NewLoc" forKey:@"Type"];
//[jsonObject setObject:metadata forKey:@"__metadata"];
[jsonObject setObject:@"Gurras" forKey:@"Description"];
[jsonObject setObject:@"Klotter" forKey:@"Category"];
[jsonObject setObject:@"smedjegatan" forKey:@"Address"];
[jsonObject setObject:@"34" forKey:@"StreetNumber"];
[jsonObject setObject:@"True" forKey:@"Feedback"];
[jsonObject setObject:@"Telefon" forKey:@"FeedbackWay"];
// ... complete the other values
// 
NSString* jsonRequest = [jsonObject JSONRepresentation];
// jsonString now contains your example strings.

NSLog(@"Request: %@", jsonRequest);

//NSURL *url = [NSURL URLWithString:@"https://mydomain.com/Method/"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

//[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];

NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

NSLog(@"returnData: %@", returnString);

我还可以添加一个如何使用javascript与服务对话的示例:

<script type="text/javascript">
var obj = { "Description": "det kanske funkar" };
$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/Errors/1.0",
        dataType: "json",
        contentType: "application/json",
        processData: true,
        data: '{"Description": "STeffeent   asdasd", "Category": "Miljö", "Address": "Bogatan","StreetNumber": "14", "Feedback": "true", "FeedbackWay": "Brev"}',
        success: function (data) {
            $("#result").text(data.Description);
        }
    });
});

0 个答案:

没有答案