Php 如何在android json中成功登录?
我正在使用登录和注册,我创建了登录类并进行了一些编码。当我尝试注册时,它会给我错误,我是android新手,所以请帮助。。 这是我的登录类:Php 如何在android json中成功登录?,php,android,json,login,Php,Android,Json,Login,我正在使用登录和注册,我创建了登录类并进行了一些编码。当我尝试注册时,它会给我错误,我是android新手,所以请帮助。。 这是我的登录类: public class Login extends AppCompatActivity { private ProgressDialog pDialog; Button login; private static final String TAG_SUCCESS = "success"; JSONParser json
public class Login extends AppCompatActivity {
private ProgressDialog pDialog;
Button login;
private static final String TAG_SUCCESS = "success";
JSONParser jsonParser = new JSONParser();
private int serverResponseCode = 0;
TextView tac1;
EditText email,pass;
private static String url_create_book = "http://cloud......com/broccoli/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
email=(EditText)findViewById(R.id.email);
pass=(EditText)findViewById(R.id.password);
tac1 = (TextView) findViewById(R.id.cAcc);
tac1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Login.this, RegistrationForm.class));
}
});
login=(Button)findViewById(R.id.lbtn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CreateNewProduct().execute();
}
});
}
class CreateNewProduct extends AsyncTask<String, String, String> {
private String eml;
private String pswrd;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
eml = email.getText().toString();
pswrd = pass.getText().toString();
//Toast.makeText(RegistrationForm.this,
//dobr, Toast.LENGTH_SHORT).show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", eml));
params.add(new BasicNameValuePair("password", pswrd));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_book,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), TypeMenu.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
在浏览器中用参数检查你的url。然后检查它是否给出正确的json值。是的,正确。请查看我的php文件。如果我手动匹配电子邮件和密码,那么它会显示“成功”。我不知道哪里错了???我可以告诉你通过截取使用此登录API。。如果您感兴趣,请在浏览器中使用参数检查您的url。然后检查它是否提供了正确的json值。是的,这是正确的。请查看我的php文件。如果我手动匹配电子邮件和密码,那么它将显示“成功”。我不知道它错在哪里???我可以告诉您通过截取使用此登录API。。如果你有兴趣
<?php
include ('config.php');
$email = $_POST['email'];
$password = $_POST['password'];
$sql = mysqli_query($conn,"SELECT * FROM UserInfo WHERE email='".$email."' AND password='".$password."'");
$check = mysqli_fetch_array($sql);
if(isset($check)){
echo 'success';
}else{
echo 'failure';
}
mysqli_close($con);
?>
E/JSON Parser: Error parsing data org.json.JSONException: Value failure of type java.lang.String cannot be converted to JSONObject
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.example.zeba.broccoli, PID: 24295
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
at com.example.zeba.broccoli.Login$CreateNewProduct.doInBackground(Login.java:125)
at com.example.zeba.broccoli.Login$CreateNewProduct.doInBackground(Login.java:82)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
D/mali_winsys: new_window_surface returns 0x3000, [540x960]-format:1
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@369d02de time:39270446
I/Process: Sending signal. PID: 24295 SIG: 9
Application terminated.