我想與我的PHP Web服務(谷歌應用程序引擎)進行通信,但它一直與身體作爲空響應,也觸發失敗()方法的改造(回調)。翻新1.9保持調用失敗,並返回空體
我調試的時候得到它失敗(),出現以下錯誤信息:
retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
首先這裏是我引發的請求,我的web服務代碼:
WebServiceManager.getInstance().registerRequest(email, password, username, firstname, lastname, new Callback<RegisterResponse>() {
@Override
public void success(RegisterResponse registerResponse, Response response) {
Toast.makeText(context, registerResponse.getToken() + "", Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(context,"error ", Toast.LENGTH_LONG).show();
}
});
這裏是我的WebServiceManager類用於管理請求和響應: http://pastebin.com/Q8bzqQ0M
並且請求和響應的模型類: http://pastebin.com/aB98Yiua
這是我的JSON請求看起來像:
{
data{
"email": "[email protected]",
"firstname": "regdrgersf",
"lastname": "regdrgersf",
"password": "hejhejhej",
"username": "regdrgersf"
}
}
我也認爲這是相關的,包括部分在那裏我收到我的web請求:
<?php
require '../functions/db.php';
$data = getJsonRequest();
$email = $data['data']['email'];
$password = $data['data']['password'];
$username = $data['data']['username'];
$firstname = $data['data']['firstname'];
$lastname = $data['data']['lastname'];
$facebookID = $data['data']['facebookID'];
$dbClass = new DBClass();
$result = $dbClass->register($email, $password, $username, $firstname, $lastname, $facebookID);
$response = array();
if ($result == $dbClass->error_input || $result == $dbClass->error_email_exists || $result == $dbClass->error_unknown || $result == $dbClass->error_db_connection) {
$response['status'] = false;
$response['error'] = $result;
} else {
$response['status'] = true;
$response['token'] = $result;
}
sendJsonResponse($response);
?>
而函數「sendJsonResponse($ response)」用於發送響應:
function sendJsonResponse($response) {
header('HTTP/1.1 200 OK');
header('Content-type: application/json');
echo json_encode($response);
}
我不明白,如果問題是在我的Java(Android)代碼和改造的地方,或者如果我在網絡服務器上做錯了什麼。
任何幫助將不勝感激。
您的json存在問題,但我沒有看到您設置的位置。 – nasch
這個異常非常具有描述性,Retrofit期待像{「key」:「value」}這樣的JSON對象,但是響應是一個簡單的字符串..您可以使用.setLogLevel(RestAdapter.LogLevel.FULL)在您的RestAdapter.Builder() – luciofm
你可以發佈你的PHP輸出的JSON嗎? –