First, make sure you get the correct JSON object from your website - try printing it as Toast.makeText() . While web browsers leave html comments, android gets it in response.
AsyncTask objects and classes are not intended to be executed as you have provided, nor can you perform any user interface operations in doInBackground() . AsyncTask is designed to not block the GUI. Here is a completely different example of how it uses the methods that you have in the AsyncTask class:
class Logging extends AsyncTask<String,String,Void>{ JSONObject json=null; String output=""; String log=StringCheck.buildSpaces(login.getText().toString()); String pas=StringCheck.buildSpaces(password.getText().toString()); String url="http://www.mastah.esy.es/webservice/login.php?login="+log+"&pass="+pas; protected void onPreExecute() { Toast.makeText(getApplicationContext(), "Operation pending, please wait", Toast.LENGTH_SHORT).show(); } @Override protected Void doInBackground(String... params) { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); request.addHeader("User-Agent", "User-Agent"); HttpResponse response; try { response = client.execute(request); BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line=""; StringBuilder result = new StringBuilder(); while ((line = br.readLine()) != null) { result.append(line); } output=result.toString(); } catch (ClientProtocolException e) { Toast.makeText(getApplicationContext(), "Connection problems", Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(getApplicationContext(), "Conversion problems", Toast.LENGTH_LONG).show(); } return null; } @Override protected void onPostExecute(Void w) { try { json = new JSONObject(output); if(json.getInt("err")==1){ Toast.makeText(getApplicationContext(), json.getString("msg"), Toast.LENGTH_LONG).show(); }else{ String id_user="-1"; Toast.makeText(getApplicationContext(), json.getString("msg"), Toast.LENGTH_LONG).show(); JSONArray arr = json.getJSONArray("data"); for(int i =0;i<arr.length();i++){ JSONObject o = arr.getJSONObject(i); id_user = o.getString("id_user"); } User.getInstance().setName(log); User.getInstance().setId(Integer.valueOf(id_user)); Intent i = new Intent(getApplicationContext(),Discover.class); startActivity(i); } } catch (JSONException e) { } super.onPostExecute(w); } }
PHP file contents:
$data = array( 'err' => 0, 'msg' => "", 'data' => array(), ); $mysqli = new MySQLi($dbhost,$dbuser,$dbpass,$dbname); if($mysqli->connect_errno){ $data['err'] = 1; $data['msg'] = "Brak polaczenia z baza"; exit(json_encode($data)); } if(isset($_GET['login']) && isset($_GET['pass'])){ $mysqli->query("SET CHARACTER SET 'utf8';"); $query = $mysqli->query("SELECT banned.id_user FROM banned JOIN user ON user.id_user = banned.id_user WHERE user.login ='{$_GET['login']}' LIMIT 1;"); if($query->num_rows){ $data['err']=1; $data['msg']="User banned"; exit(json_encode($data)); }else{ $query = $mysqli->query("SELECT login FROM user WHERE login='{$_GET['login']}' LIMIT 1;"); if($query->num_rows){ $query = $mysqli->query("SELECT pass FROM user WHERE pass ='{$_GET['pass']}' LIMIT 1;"); if($query->num_rows){ $data['msg']="Logged IN!"; $query = $mysqli->query("SELECT id_user FROM user WHERE login='{$_GET['login']}' LIMIT 1;"); $data['data'][]=$query->fetch_assoc(); exit(json_encode($data)); }else{ $data['err']=1; $data['msg']="Wrong login credentials."; exit(json_encode($data)); } }else{ $data['err']=1; $data['msg']="This login doesn't exist."; exit(json_encode($data)); } } }else{ $data['err']=1; $data['msg']="Wrong login credentials"; exit(json_encode($data)); }
I created there a small $data dictionary for my application. I used his err key as a flag to find out if there was any error, msg , to inform the user about the results of work and data to send JSON objects.
The thing you would like to do with if(response == true) , if it existed, is similar to the construct I used in my onPostExecute(Void w) method in AsyncTask :
if(json.getInt("err")==1){ //something went wrong }else{ //everything is okay, get JSON, inform user, start new Activity }
It also uses the $data['data'] method to get a JSON response:
if($query->num_rows){ while($res=$query->fetch_assoc()){ $data['data'][]=$res; } exit(json_encode($data)); }