iPhone http发布请求字段和值

时间:2014-02-21 21:55:14

标签: php iphone http-post

我需要从.php获取JSON,我需要帮助知道如何填写所需的字段,因为我之前没有这样做,并且真的不完全理解.php页面。以下是.php链接的详细信息

jQuery(function($){

function get_current_show() {
//Get the current show data
$.post(crudScriptURL, {"crud-action" : "read", "read-type" : "current"}, function (currentShowJSON) {

    var schedule = $.parseJSON(currentShowJSON);
    var outputHTML = '';

    var currentShow = schedule['current-show'];
    if (currentShow.showName){
        var currentShowName = currentShow.showName;
        var imageURL = currentShow.imageURL;
        var linkURL = currentShow.linkURL;
        var startClock = currentShow.startClock;
        var endClock = currentShow.endClock;

        if (linkURL){
            currentShowName = '<a href="'+linkURL+'">'+currentShowName+'</a>';
        }



        outputHTML += '<h3 class="current-show">'+currentShowName+'</h3>';
        outputHTML += '<span>'+startClock + ' - ' + endClock + '</span>';

        if (imageURL){
            if (linkURL){
                outputHTML += '<a href="'+linkURL+'"><img class="showtime-image-thumbnail" src="'+imageURL+'" alt="'+currentShow.showName+'" /></a>';
            } else {
                outputHTML += '<img class="showtime-image-thumbnail" src="'+imageURL+'" alt="'+currentShow.showName+'" />';
            }
        }

    } else {

        outputHTML += '<h3 class="current-show">'+currentShow+'<h3>';

    }

    var upcomingShow = schedule['upcoming-show'];
    if (upcomingShow){
        var upcomingShowName = upcomingShow.showName;
        var upcomingShowLink = upcomingShow.linkURL;
        var upcomingStartClock = upcomingShow.startClock;
        var upcomingEndClock = upcomingShow.endClock;

        if (upcomingShowLink){
            outputHTML += '<h3 class="upcoming-show"><strong>Up next:</strong> <a href="'+upcomingShowLink+'">'+upcomingShowName+'</a></h3>';
        } else {
            outputHTML += '<h3 class="upcoming-show"><strong>Up next:</strong> '+upcomingShowName+'</h3>';
        }

        outputHTML += '<span>'+upcomingStartClock + ' - ' + upcomingEndClock + '</span>';

    }

    $('.showtime-now-playing').html(outputHTML);

    //Set a timer to update the widget every 30 seconds
    setTimeout (get_current_show, (30 * 1000));

});

}

get_current_show();


});

这就是我正在尝试的事情

NSString *urlString = [NSString stringWithFormat:@"http://selectukradio.com/wp-content/plugins/showtime/showtime.js"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:@"crud-action" forHTTPHeaderField:@"read"];
[request setValue:@"read-type" forHTTPHeaderField:@"current"];
[request setValue:@"http://selectukradio.com/wp-content/plugins/showtime/crud.php" forHTTPHeaderField:@"currentShowJSON"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
    NSLog(@"Response: %@", result);

    //here you get the response
}

任何想法我做错了什么? 我在日志中收到的回复是

2014-02-21 22:06:47.510 request[579:70b] Response Code: 200
2014-02-21 22:06:47.511 request[579:70b] Response: jQuery(function($){

function get_current_show() {
//Get the current show data
$.post(crudScriptURL, {"crud-action" : "read", "read-type" : "current"}, function (currentShowJSON) {

    var schedule = $.parseJSON(currentShowJSON);
    var outputHTML = '';

    var currentShow = schedule['current-show'];
    if (currentShow.showName){
        var currentShowName = currentShow.showName;
        var imageURL = currentShow.imageURL;
        var linkURL = currentShow.linkURL;
        var startClock = currentShow.startClock;
        var endClock = currentShow.endClock;

        if (linkURL){
            currentShowName = '<a href="'+linkURL+'">'+currentShowName+'</a>';
        }



        outputHTML += '<h3 class="current-show">'+currentShowName+'</h3>';
        outputHTML += '<span>'+startClock + ' - ' + endClock + '</span>';

        if (imageURL){
            if (linkURL){
                outputHTML += '<a href="'+linkURL+'"><img class="showtime-image-thumbnail" src="'+imageURL+'" alt="'+currentShow.showName+'" /></a>';
            } else {
                outputHTML += '<img class="showtime-image-thumbnail" src="'+imageURL+'" alt="'+currentShow.showName+'" />';
            }
        }

    } else {

        outputHTML += '<h3 class="current-show">'+currentShow+'<h3>';

    }

    var upcomingShow = schedule['upcoming-show'];
    if (upcomingShow){
        var upcomingShowName = upcomingShow.showName;
        var upcomingShowLink = upcomingShow.linkURL;
        var upcomingStartClock = upcomingShow.startClock;
        var upcomingEndClock = upcomingShow.endClock;

        if (upcomingShowLink){
            outputHTML += '<h3 class="upcoming-show"><strong>Up next:</strong> <a href="'+upcomingShowLink+'">'+upcomingShowName+'</a></h3>';
        } else {
            outputHTML += '<h3 class="upcoming-show"><strong>Up next:</strong> '+upcomingShowName+'</h3>';
        }

        outputHTML += '<span>'+upcomingStartClock + ' - ' + upcomingEndClock + '</span>';

    }

    $('.showtime-now-playing').html(outputHTML);

    //Set a timer to update the widget every 30 seconds
    setTimeout (get_current_show, (30 * 1000));

});

}

get_current_show();


});

我需要获取currentShow.imageURL和currentShow.showName等 网页的源代码显示了这个

<script>crudScriptURL = "http://selectukradio.com/wp-content/plugins/showtime/crud.php"</script>     <script type="text/javascript" src="http://selectukradio.com/wp-content/plugins/showtime/showtime.js" ></script>

1 个答案:

答案 0 :(得分:0)

我通过使用AFNetworking解决了这个问题,下面是帮助其他人的代码

NSString *URLString = @"http://selectukradio.com/wp-content/plugins/showtime/crud.php";
NSDictionary *parameters = @{@"crud-action" : @"read", @"read-type" : @"current"};
NSMutableArray *names = [[NSMutableArray alloc]init];
NSMutableArray *images = [[NSMutableArray alloc]init];


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if ([responseObject isKindOfClass:[NSDictionary class]]) {
        NSLog(@"its a dictionary");


        for (NSString *key in [responseObject allKeys])
        {
            NSDictionary *feed = [responseObject objectForKey:key];
            NSString *name = [feed objectForKey:@"showName"];
            NSString *final = [name stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
            NSString *image = [feed objectForKey:@"imageURL"];
            [names addObject:final];
            [images addObject:image];
            //do stuff with feed.
        }
        NSLog(@" First dj: %@",[names objectAtIndex:0]);
        NSLog(@" First dj image: %@", [images objectAtIndex:0]);
        NSLog(@"Next up dj: %@", [names objectAtIndex:1]);
        NSLog(@"Next dj image: %@", [images objectAtIndex:1]);


    }

 //   NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];