I just want to login to my Data Base using php code and view the result in just a Toast
but I don't know where I should looking for the error when appear FATAL EXCEPTION
Android Code:
package com.example.e_music;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
EditText username,password;
Button btnLogin,btnReg;
JSONParser jp=new JSONParser();
private static final String sTag_Login="login";
private static String loginURL = "http://mwssong.esy.es/android/Login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.txtUN);
password = (EditText)findViewById(R.id.txtP);
btnLogin = (Button)findViewById(R.id.btnLogin);
btnReg = (Button)findViewById(R.id.btnReg);
btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent iReg = new Intent(getApplicationContext(), Registeration.class);
iReg.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(iReg);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if ( ( !username.getText().toString().equals("")) && ( !password.getText().toString().equals("")) )
{
new Connect().execute();
}
else if ( ( !username.getText().toString().equals("")) )
{
Toast.makeText(getApplicationContext(),
"Password field empty", Toast.LENGTH_SHORT).show();
}
else if ( ( !password.getText().toString().equals("")) )
{
Toast.makeText(getApplicationContext(),
"User Name field empty", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),
"User Name and Password field are empty", Toast.LENGTH_SHORT).show();
}
}
});
}
class Connect extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... arg0) {
List<NameValuePair> params= new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username.getText().toString()));
params.add(new BasicNameValuePair("password", password.getText().toString()));
JSONObject json=JSONParser.getJSONFromUrl(loginURL, params);
try{
String login= json.getString(sTag_Login);
if(login=="Admin"){
Toast.makeText(getApplicationContext(), "Welcome Admin", Toast.LENGTH_LONG).show();
}
else if(login=="Customer"){
Toast.makeText(getApplicationContext(), "Welcome", Toast.LENGTH_LONG).show();
}
{
Toast.makeText(getApplicationContext(), "error ", Toast.LENGTH_LONG).show();
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
}
and this's my php code:
<?php
$response = array();
$db = mysqli_connect('mysql.hostinger.ae','u641845309_ur','q1p0w2o9','u641845309_song');
$username=$_POST['username'];
$password=$_POST['password'];
$sql="SELECT * FROM customer WHERE UName='$username' and Password='$password'";
$result=mysqli_query($db,$sql);
if($result)
{
while($row = mysqli_fetch_array($result)) {
if(strtolower($username)=='admin')
$response["login"] = "Admin";
else
$response["login"] = "Customer";
}
}
else
{
$response["login"] ="error";
}
echo json_encode($response);
?>
JSON Code:
package com.example.e_music;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public static JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}