gpt4 book ai didi

java - Bug : E/JSON Parser﹕ Error parsing data org. json.JSONException : Value Hello of type java. lang.String无法转换为JSONObject

转载 作者:行者123 更新时间:2023-11-30 02:14:51 25 4
gpt4 key购买 nike

当我点击我的登录按钮时,我得到了这个,我不知道为什么,我不知道为什么它在错误中说你好,我搜索了我的整个项目,找不到“你好”的出现.此错误还有其他原因吗?

public class Login extends Activity implements OnClickListener {

private EditText user, pass;
private Button mSubmit, mRegister;

// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();




private static final String LOGIN_URL = "http://xxxxxxxxxxxx";



private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

// setup input fields
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);

// setup buttons
mSubmit = (Button) findViewById(R.id.login);
mRegister = (Button) findViewById(R.id.register);

// register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;

default:
break;
}
}

class AttemptLogin extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));

Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);

// check your log for json response
Log.d("Login attempt", json.toString());

// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();

Intent i = new Intent(Login.this, MainActivity.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;

}

protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}

}

}

}

编辑代码

<?php
if (!isset($_SESSION)) {
session_start();
}


//load and connect to MySQL database stuff
require("config.inc.php");




if (!empty($_POST)) {
$query = "SELECT id,password, username, iv FROM users WHERE username = :username";




$username = $_POST['username'];

//encrypting the username and password to compare to the ones stored in the databaase



$query_params = array(
':username' => $username,
);

try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);


}
catch (PDOException $ex) {

$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die("Failed to run query: " . $ex->getMessage());

}




//fetching all the rows from the query

$password = $_POST['password'];

$row = $stmt->fetch();
$key = $row['iv'];
$encrypted_password = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret_key, $password, MCRYPT_MODE_CBC, $key);
if ($row) {
if ($encrypted_password == $row['password']) {


$login_ok = true;
}
}

if ($login_ok) {
$response["success"] = 1;
$response["message"] = "Login successful!";
die(json_encode($response));
echo json_encode($response);



} else {
$response["success"] = 0;
$response["message"] = "Invalid Credentials!";
die(json_encode($response));
}

} else {
?>
<h1>Login</h1>
<form action="login.php" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />
Password:<br />
<input type="password" name="password" placeholder="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>
<?php
}

?>

最佳答案

您的 JSONParser 类可能有问题,或者服务器的响应可能返回“hello”。您能否也发布该类(class)以使事情更清楚。

关于java - Bug : E/JSON Parser﹕ Error parsing data org. json.JSONException : Value Hello of type java. lang.String无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com