解析数据时出错org.json.JSONException:值<br/>字符串无法转换为JSONObject

时间:2015-05-13 12:23:02

标签: php android json httprequest

我知道这个问题已被回答了很多次。我尝试了不同的链接,但我无法解决这个问题。

Java代码

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username",u));
params.add(new BasicNameValuePair("Email",e));
params.add(new BasicNameValuePair("Password", p));
params.add(new BasicNameValuePair("Confirmpassword", cp));
JSONObject json = jsonParser.makeHttpRequest(url_logindetails, "POST", params); 

PHP代码:     

$response = array(); 
// check for required fields 
if (isset($_POST['Username']) || isset($_POST['Email'])|| isset($_POST['Password']) ||isset($_POST['Confirmpassword']) ) { 

  $Username = $_POST['Username']; 
  $Email = $_POST['Email']; 
  $Password = $_POST['Password']; 
  $Confirmpassword = $_POST['Confirmpassword'];

  // include db connect class 
  require_once __DIR__ . '/db_connect.php'; 
  // connecting to db 
  db = new DB_CONNECT(); 
  // mysql inserting a new row 
  $result = mysql_query("INSERT INTO login(UserName, email,password,confirmpassword VALUES('$Username','$Email','$Password','$Confirmpassword')"); 
  // check if row inserted or not 

  if ($result) { 
    // successfully inserted into database 
    $response["success"] = 1; 
    $response["message"] = "Company successfully added."; 
    // echoing JSON response 
    echo json_encode($response); 
  } else { 
    // failed to insert row 
    $response["success"] = 0; 
    $response["message"] = "Oops! An error occurred."; 
    // echoing JSON response 
    echo json_encode($response); 
  }
} else { 
  // required field is missing 
  $response["success"] = 0; 
  $response["message"] = "Required field(s) is missing"; 
  // echoing JSON response 
  echo json_encode($response); 
} 
?>

1 个答案:

答案 0 :(得分:0)

BasicNameValuePair(“用户名”,你)它不对 BasicNameValuePir( “用户名”)