2014-02-06 62 views
0

我想做一個註冊和登錄功能,但有JSON和AsyncTask的問題。這是logcat錯誤。JSON解析和AsyncTask錯誤

02-06 04:18:08.857: E/JSON(1160): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">n<HTML><HEAD><TITLE>Not Found</TITLE>n<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>n<BODY><h2>Not Found</h2>n<hr><p>HTTP Error 404. The requested resource is not found.</p>n</BODY></HTML>n 
02-06 04:18:08.857: E/JSON Parser(1160): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 
02-06 04:18:08.887: W/dalvikvm(1160): threadid=11: thread exiting with uncaught exception (group=0x41465700) 
02-06 04:18:08.937: E/AndroidRuntime(1160): FATAL EXCEPTION: AsyncTask #1 
02-06 04:18:08.937: E/AndroidRuntime(1160): java.lang.RuntimeException: An error occured while executing doInBackground() 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at android.os.AsyncTask$3.done(AsyncTask.java:299) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at java.lang.Thread.run(Thread.java:841) 
02-06 04:18:08.937: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at my.fyp.inticlassifieds.RegisterTask.doInBackground(RegisterTask.java:76) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at my.fyp.inticlassifieds.RegisterTask.doInBackground(RegisterTask.java:1) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
02-06 04:18:08.937: E/AndroidRuntime(1160):  ... 4 more 

這是用的AsyncTask類使用:

public class RegisterTask extends AsyncTask<String, Void, Integer> { 

private ProgressDialog progressDialog; 
private RegisterActivity activity; 
private int id = -1; 
private JSONParser jsonParser; 
private static String loginURL = "http://10.2.2.0/project/index.php"; 
private static String registerURL = "http://10.2.2.0/project/index.php"; 
private static String KEY_SUCCESS = "success"; 
private static String KEY_ERROR = "error"; 
private static String KEY_ERROR_MSG = "error_msg"; 
private static String KEY_UID = "uid"; 
private static String KEY_NAME = "name"; 
private static String KEY_EMAIL = "email"; 
private static String KEY_CREATED_AT = "created_at"; 
private int responseCode = 0; 

public RegisterTask(RegisterActivity activity, ProgressDialog progressDialog) { 
    this.activity = activity; 
    this.progressDialog = progressDialog; 
} 

@Override 
protected void onPreExecute() { 
    progressDialog.show(); 
    progressDialog.dismiss(); 
} 

@Override 
protected Integer doInBackground(String... arg0) { 
    EditText userName = (EditText) activity 
      .findViewById(R.id.registerEmail); 
    EditText passwordEdit = (EditText) activity 
      .findViewById(R.id.registerPassword); 
    EditText userContact = (EditText) activity 
      .findViewById(R.id.registerContact); 
    String contact = userContact.getText().toString(); 
    String email = userName.getText().toString(); 
    String password = passwordEdit.getText().toString(); 
    Log.v(email, password); 
    UserFunctions userFunction = new UserFunctions(); 
    JSONObject json = userFunction.registerUser(email, password, contact); 

    // check for login response 
    try { 
     if (json.getString(KEY_SUCCESS) != null) { 
      // registerErrorMsg.setText(""); 
      String res = json.getString(KEY_SUCCESS); 
      if (Integer.parseInt(res) == 1) { 
       // user successfully registred 
       // Store user details in SQLite Database 
       DatabaseHandler db = new DatabaseHandler(
         activity.getApplicationContext()); 
       JSONObject json_user = json.getJSONObject("user"); 

       // Clear all previous data in database 
       userFunction.logoutUser(activity.getApplicationContext()); 
       db.addUser(json_user.getString(KEY_EMAIL), 
         json.getString(KEY_UID), 
         json_user.getString(KEY_CREATED_AT)); 
       // successful registration 
       responseCode = 1; 
      } else { 
       // Error in registration 
       responseCode = 0; 
      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return responseCode; 
} 

@Override 
protected void onPostExecute(Integer responseCode) { 
    EditText userName = (EditText) activity 
      .findViewById(R.id.registerEmail); 
    EditText passwordEdit = (EditText) activity 
      .findViewById(R.id.registerPassword); 
    String s = userName.getText().toString(); 

    if (responseCode == 1) { 
     progressDialog.dismiss(); 
     activity.registerReport(responseCode); 
     userName.setText(""); 
     passwordEdit.setText(""); 
    } 
    if (responseCode == 0) { 
     progressDialog.dismiss(); 
     activity.registerReport(responseCode); 
    } 
} 
} 

或可能的誤差可能是從PHP本身?

<?php 
/** 
* File to handle all API requests 
* Accepts GET and POST 
* 
* Each request will be identified by TAG 
* Response will be JSON data 

    /** 
* check for POST request 
*/ 
if (isset($_POST['tag']) && $_POST['tag'] != '') { 
    // get tag 
    $tag = $_POST['tag']; 

// include db handler 
require_once 'include/DB_Functions.php'; 
$db = new DB_Functions(); 

// response Array 
$response = array("tag" => $tag, "success" => 0, "error" => 0); 

// check for tag type 
if ($tag == 'login') { 
    // Request type is check Login 
    $email = $_POST['email']; 
    $password = $_POST['password']; 

    // check for user 
    $user = $db->getUserByEmailAndPassword($email, $password); 
    if ($user != false) { 
     // user found 
     // echo json with success = 1 
     $response["success"] = 1; 
     $response["user"]["email"] = $user["email"]; 
     $response["user"]["contact"] = $user["contact_no"]; 
     $response["user"]["created_at"] = $user["year_joined"]; 

     echo json_encode($response); 
    } else { 
     // user not found 
     // echo json with error = 1 
     $response["error"] = 1; 
     $response["error_msg"] = "Incorrect email or password!"; 
     echo json_encode($response); 
    } 
} else if ($tag == 'register') { 
    // Request type is Register new user 
    $email = $_POST['email']; 
    $password = $_POST['password']; 
    $contact = $_POST['contact']; 

    // check if user is already existed 
    if ($db->isUserExisted($email)) { 
     // user is already existed - error response 
     $response["error"] = 2; 
     $response["error_msg"] = "User already existed"; 
     echo json_encode($response); 
    } else { 
     // store user 
     $user = $db->storeUser($email, $contact_no, $password); 
     if ($user) { 
      // user stored successfully 
      $response["success"] = 1; 
      $response["user"]["email"] = $user["email"]; 
      $response["user"]["contact"] = $user["contact_no"]; 
      $response["user"]["created_at"] = $user["year_joined"]; 

      echo json_encode($response); 
     } else { 
      // user failed to store 
      $response["error"] = 1; 
      $response["error_msg"] = "Error occured in Registartion"; 
      echo json_encode($response); 
     } 
    } 
} else { 
    echo "Invalid Request"; 
} 
} else { 
    echo "Access Denied"; 
} 
?> 

更改鏈接,現在我有這個錯誤。

02-06 09:45:18.168: E/JSON(1697): <br />n<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>n<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Warning: require_once(include/DB_Functions.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in D:\Softwares\wamp\www\project\index.php on line <i>17</i></th></tr>n<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>n<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>n<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>692600</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Softwares\wamp\www\project\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>n</table></font>n<br />n<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>n<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'include/DB_Functions.php' (include_path='.;C:\php\pear') in D:\Softwares\wamp\www\project\index.php on line <i>17</i></th></tr>n<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>n<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>n<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>692600</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Softwares\wamp\www\project\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>n</table></font>n 
+0

你有NPE在RegisterTask.java:76 ..這行是什麼? –

+0

@ M.Sameer如果(json.getString(KEY_SUCCESS)! = null) – Benz

回答

0

你指向一個錯誤的URL:404 - 未找到,

這是你試圖解析什麼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">n<HTML><HEAD><TITLE>Not Found</TITLE>n<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>n<BODY><h2>Not Found</h2>n<hr><p>HTTP Error 404. The requested resource is not found.</p>n</BODY></HTML> 

正如你所看到的不是一個JSON ,而是一個HTML頁面。直接從瀏覽器測試指向它的網址。你應該看到一個JSON格式的字符串。

+0

我應該輸入什麼網址? – Benz

+0

@firaga我猜你的php頁面的網址。 – Enrichman

0

使用錯誤的網址。您正在獲取404錯誤頁面作爲響應,並且您試圖將html解析爲JSON。

0

價值!DOCTYPE」檢查您的網址和servlet或防火牆CFG