私はiOSの初心者です。テーブルからデータを取得し、結果をJSONで送信する単純なWebサービスがあります。iOSからそのWebサービスと通信して、JSON応答を受信しようとしていますが、問題が発生しています。これは私が受け取るエラーです:
Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html" UserInfo=0x7598e70
これが私のコードスニペットです:
PHP Webサービス:
$stmt = "SELECT STORE_TYPE, STORE_NAME FROM STORE WHERE STORE_ZIP = $zip";
$result = mysqli_query($this->databaseconnection, $stmt);
$storelist = array();
$store = array();
$jsondata;
while ($row = mysqli_fetch_assoc($result)) {
$store['STORE_TYPE'] = $row['STORE_TYPE'];
$store['STORE_NAME'] = $row['STORE_NAME'];
array_push($storelist,$store);
}
$jsondata = json_encode($storelist);
echo $jsondata;
ブラウザからphpを実行すると、次の結果が得られます。
[{"STORE_TYPE":"GROCERY","STORE_NAME":"Walmart"},{"STORE_TYPE":"BAKERY","STORE_NAME":"Lanes Bakery"},{"STORE_TYPE":"GROCERY","STORE_NAME":"Copps"}]
Webサービスと通信するためのiOSコードスニペット:
NSURL *url = [NSURL URLWithString:@"http://localhost/~Sandeep/store/store.php?rquest=getstores&zip=53715"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
多くのチュートリアルを見ましたが、phpの配列で「json_encode」を実行するとデータがJSON形式でエンコードされ、その「echo」がエンコードされたJSONを応答として送信する方法であると言われています。何らかの理由で、私のiOSはそれをJSONとして認識していません。ここで何が欠けている/間違っているのかわかりません。
これについてのご意見に心から感謝いたします。
ありがとう!