我正在获取>>>每次都无法将java.lang.String类型的值br转换为JSONArray。<<<<
令人困惑。
请帮帮我。
//注册活动类//
package com.example.reean.guidetour;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Attributes;
public class Register_Activity extends AppCompatActivity {
Button reg_bn;
EditText Name, Email, UserName, Password, ConPassword;
String name, email, username, password, conpass;
AlertDialog.Builder builder;
String reg_url = "http://192.168.0.104/register.php";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_);
reg_bn = (Button) findViewById(R.id.bRegister);
Name = (EditText) findViewById(R.id.etName);
UserName = (EditText) findViewById(R.id.etUserName);
Email = (EditText) findViewById(R.id.etEmail);
Password = (EditText) findViewById(R.id.etPassword);
ConPassword = (EditText) findViewById(R.id.etPassword_confirm);
builder = new AlertDialog.Builder(Register_Activity.this);
reg_bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = Name.getText().toString();
email = Email.getText().toString();
username = UserName.getText().toString();
password = Password.getText().toString();
conpass = ConPassword.getText().toString();
if (name.equals("") || email.equals("") || username.equals("") || password.equals("") || conpass.equals("")) {
builder.setTitle("Something went wrong!");
builder.setMessage("Fill all blank fields");
displayAlert("Input_Error");
}
else
{
if (!(password.equals(conpass)))
{
builder.setTitle("Something went wrong!");
builder.setMessage("Password Mismatched!");
displayAlert("Input_Error");
}
else {
StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
builder.setTitle("Server Response ");
builder.setMessage(message);
displayAlert(code);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("user_name", username);
params.put("password", password);
return params;
}
};
MySingleton.getmInstance(Register_Activity.this).addToRequestQueue(stringRequest);
}
}
};
});
}
public void displayAlert(final String code)
{
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (code.equals("Input_Error")) {
Password.setText("");
ConPassword.setText("");
} else if (code.equals("Register_Success")){
startActivity(new Intent(Register_Activity.this,Login_Activity.class));
//finish();
}
else if (code.equals("Register_Failed"))
{
Name.setText("");
Email.setText("");
UserName.setText("");
Password.setText("");
ConPassword.setText("");
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
//注册//的PHP代码
<?php
require "init,php";
$name =$_POST["name"];
$email =$_POST["email"];
$user_name =$_POST["user_name"];
$password =$_POST["password"];
$sql = "select * from user_info where email like '".$email."';";
$result = mysqli_query($con,$sql);
$response = array();
if (mysqli_num_rows($result>0)
{
$code = "Register_Failed";
$message = "User already exist... ";
array_push($response,array("code"=>$code,"meesage"=>$message));
echo json_encode($response);
}
else
{
$sql = "insert into user_info values('".$name."','".$email."','".$user_name."','".$password."');";
$result = mysqli_query($con,$sql);
$code = "Register_Success";
$message = "Thank you. Now you can login... ";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode($response);
}
mysqli_close($con);
?>`