我正在开发一个通过JSON与mysql数据库通信的android注册系统。宁静的Web服务是用PHP和Slim库编写的。我已经使用Advanced Rest Client App测试了Web服务,通过将json有效负载数据解析到它中,它与下面显示的结果完全一致。目前我正在尝试将用户数据从Android应用程序解析到服务器,并显示此错误(Error parsing data org.json.JSONException: Value <html><head><title>Slim of type java.lang.String cannot be converted to JSONObject)
。我会有人告诉我我做错了什么因为我觉得错误来自我的 JSONParser 类和用户功能。提前谢谢。
这是我的测试结果
{ “标记”: “注册”, “成功”:1, “错误”:0, “UID”: “547d92b0480711.90973742”,
“结果”:{ “姓”: “米什”, “姓”: “哈里”, “电子邮件”: “mishael19@gmail.com”}}
这是JSONParser
public class JSONParser2 {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser2() {
}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params){
try{
if(method.equals("POST")){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
这是我的用户功能
public class UserFunctions {
private JSONParser2 jsonParser2;
private static String signupURL = "http://globalpadtutorials.globalpad.info/loginSignup/v1/signup";
private static String signup_tag = "signup";
private static String postMethod = "POST";
public UserFunctions(){
jsonParser2 = new JSONParser2();
}
public JSONObject signupUser(String fname, String lname, String email, String pwd){
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", signup_tag));
params.add(new BasicNameValuePair("firstname", fname));
params.add(new BasicNameValuePair("lastname", lname));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", pwd));
JSONObject json = jsonParser2.makeHttpRequest(signupURL, postMethod, params);
Log.e("JSON", json.toString());
return json;
}
}
错误堆栈
12-02 11:04:19.681: E/JSON Parser(10024): Error parsing data org.json.JSONException: Value <html>
<head><title>Slim of type java.lang.String cannot be converted to JSONObject
PHP代码
function createUser(){
$request = \Slim\Slim::getInstance()->request();
$resp = \Slim\Slim::getInstance()->response();
$resp['Content-Type'] = 'application/json; charset=utf-8';
$body = $request->getBody();
$users = json_decode($body);
$dbhandler = new DbHandler();
$tag = $users->tag;
$fname = $users->firstname;
$lname = $users->lastname;
$email = $users->email;
$pwd = $users->password;
$response = array("tag" => $tag, "success" => 0, "error" => 0);
if($tag == 'signup') {
if ($dbhandler->userExisted($email)) {
$response["error"] = 2;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
$result = $dbhandler->addUser($fname, $lname, $email, $pwd);
if ($result != false) {
$response["success"] = 1;
var_dump($result);
$response["uid"] = $result["user_id"];
$response["result"]["firstname"] = $result["firstname"];
$response["result"]["lastname"] = $result["lastname"];
$response["result"]["email"] = $result["email"];
echo json_encode($response);
} else {
$response["error"] = 1;
$response["error_msg"] = "An error occured during sign up";
echo json_encode($response);
}
}
} else{
$response["error"] = 1;
$response["error_msg"] = "Invalid tag request: ".$tag;
echo json_encode($response);
}
}
html错误
Slim Application Errorbody {margin:0; padding:30px; font:12px / 1.5 Helvetica,Arial,Verdana,sans-serif;} h1 {margin:0; font-size:48px; font-weight:normal; line -height:48px;} strong {display:inline-block; width:65px;}
由于以下错误,应用程序无法运行:
#0 /home/globa390/public_html/globalpadtutorials/loginSignup/v1/index.php(63): Slim\Slim::handleErrors(8, 'Trying to get p...', '/home/globa390/...', 63, Array) 12-02 11:55:50.366: E/JSON(27492): #1 [internal function]: createUser() 12-02 11:55:50.366: E/JSON(27492): #2 /home/globa390/public_html/globalpadtutorials/loginSignup/library/Slim/Route.php(462): call_user_func_array('createUser', Array) 12-02 11:55:50.366: E/JSON(27492): #3 /home/globa390/public_html/globalpadtutorials/loginSignup/library/Slim/Slim.php(1326): Slim\Route->dispatch() 12-02 11:55:50.366: E/JSON(27492): #4 /home/globa390/public_html/globalpadtutorials/loginSignup/library/Slim/Middleware/Flash.php(85): Slim\Slim->call() 12-02 11:55:50.366: E/JSON(27492): #5 /home/globa390/public_html/globalpadtutorials/loginSignup/library/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call() 12-02 11:55:50.366: E/JSON(27492): #6 /home/globa390/public_html/globalpadtutorials/loginSignup/library/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call() 12-02 11:55:50.366: E/JSON(27492): #7 /home/globa390/public_html/globalpadtutorials/loginSignup/library/Slim/Slim.php(1271): Slim\Middleware\PrettyExceptions->call() 12-02 11:55:50.366: E/JSON(27492): #8 /home/globa390/public_html/globalpadtutorials/loginSignup/v1/index.php(12): Slim\Slim->run() 12-02 11:55:50.366: E/JSON(27492): #9 {main}
答案 0 :(得分:3)
public JSONObject signupUser(String fname, String lname, String email, String pwd){
List<NameValuePair> params = new ArrayList<NameValuePair>(5);
params.add(new BasicNameValuePair("tag", signup_tag));
params.add(new BasicNameValuePair("firstname", fname));
params.add(new BasicNameValuePair("lastname", lname));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", pwd));
String jsonstr = jsonParser2.makeHttpRequest(signupURL, postMethod, params);
//cast String into jsonobject
JSONObject json = new JSONObject(jsonStr);
return json;
}
答案 1 :(得分:1)
您的url返回 html 页面,其中包含404错误。这就是为什么你不能把它解析为json。
答案 2 :(得分:1)
我也有同样的问题。向您的服务器管理员询问使用数据库进行Web和移动的权限。之后将你的php文件粘贴到public_html中。