使用ASIHTTPRequest的iOS-POST请求

时间:2013-02-15 17:18:52

标签: json ios6 asihttprequest

我在MAC上通过MAMP建立了服务器。有人在下面编写了PHP脚本:

header("Content-Type:text/html; charset=utf-8");
if(!isset($_COOKIE['hostname']))$hostname = "http://127.0.0.1";
    else $hostname = $_COOKIE['hostname'];
if(!isset($_COOKIE['username']))$username = "root";
    else $username = $_COOKIE['username'];
if(!isset($_COOKIE['password']))$password = "";
    else $password = $_COOKIE['password'];

if(!isset($_POST['operate'])){
    echo "Please Input:";
    print(json_encode("Error:Please Input:"));
    return false;
}
else{
    $operate = $_POST['operate'];
    switch ($operate){
        case "login":
            login($_POST['hostname'],$_POST['username'],$_POST['password']);
            break;
        case "clean":
            clean();
            break;
        case "search":
            search($_POST['form'], $_POST['field'], $_POST['value']);
            break;
        case "read":
            if(!isset($_POST['num']))read1($_POST['form']);
            else read2($_POST['$form'], $_POST['$base'], $_POST['$num']);
            break;
        case "alter":
            alter($_POST['form'], $_POST['field'], $_POST['id'],$_POST['value']);
            break;
        case "delete":
            delete($_POST['form'],$_POST['id']);
            break;
        case "insert":
            if(isset($_POST['device']))insert1($_POST['form'],$_POST['device'], $_POST['port'], $_POST['vlan'], $_POST['singleMulti'], $_POST['function'], $_POST['destDevice']);
            else insert2();
            break;
        default:
            echo "Please Input:";
            print(json_encode("Error:Please Input:"));
            return false;

    }
}

function login($hostname,$username,$password){
//Login
    $link = @mysql_connect($hostname,$username,$password);
    if(!$link){
        echo "Fail to connect the DataBase";
        print(json_encode("Error:Fail to connect the DataBase"));
        return false;
    }
    mysql_query("set names utf8");
    mysql_select_db("qrcode");

    setcookie("hostname", $hostname, time()+ 9999999);
    setcookie("username", $username, time()+ 9999999);
    setcookie("password", $password, time()+ 9999999);
    mysql_close();
    return true;
}
function search($form, $field, $value){
//query
    login($hostname,$username,$password);
    $sql="select * from $form where $field = '$value'";
    $result = @mysql_result($sql);

    if(!$result){
        echo "Fail to query the DataBase";
        print(json_encode("Error:Fail to query the DataBase"));
        return false;

    }
    while($e=mysql_fetch_assoc($result))
        $output[] =$e;  
    echo json_encode($output);  
    mysql_close();
    return true;
}

function read1($form){
    if(read2($form, 0, 5))return true;      
    else return false;
}

    //Clean ...............
//Load.................
    //Alter................
//Delete...............
//Insert...............

我使用ASIHTTPRequest,代码如下:

NSURL *url=[NSURL URLWithString:@"http://localhost/QRcode.php"];
ASIFormDataRequest *request=[[ASIFormDataRequest alloc]initWithURL:url];

[request setRequestMethod:@"POST"];
[request setDelegate:self];

[request setPostValue:@"login" forKey:@"operate"];
[request setPostValue:@"localhost" forKey:@"hostname"];
[request setPostValue:@"root" forKey:@"username"];
[request setPostValue:@"root" forKey:@"password"];

[request setPostValue:@"search" forKey:@"operate"];
[request setPostValue:@"5020" forKey:@"form"];
[request setPostValue:@"id" forKey:@"field"];
[request setPostValue:@"1" forKey:@"value"];

[request startSynchronous];
NSLog(@"%@",[request responseString]);

我非常确定我的数据库中存在“5020”表单,但我得到了这个结果:

Fail to query the DataBase Error:\u6570\u636e\u5e93\u67e5\u8be2\u5931\u8d25"

我尝试搜索其他表单,但它仍然显示同样的错误。

谢谢。

1 个答案:

答案 0 :(得分:0)

解决了......像u\6570\u5647\u......这样的字符串是UTF-8编码,你只需要将你的应用程序的语言设置更改为中文然后所有奇怪的字符串都消失了.....