title: android connecting with php /mysql using json the select query do not work
body: <p>i am creating a simple android application that add user (register)  and retrieve user(login)</p>
  
  <p>the first action work perfectly but the second force close and crush the application.</p>
  
  <p>can anyone help me???</p>
  
  <h1>logCat</h1>
  
  <pre><code>04-02 08:24:18.532: E/JSON Parser(2380): Error parsing data org.json.JSONException: Value &lt;br of type java.lang.String cannot be converted to JSONObject
  04-02 08:24:18.536: W/dalvikvm(2380): threadid=12: thread exiting with uncaught exception (group=0xa4c06648)
  04-02 08:24:18.568: E/AndroidRuntime(2380): FATAL EXCEPTION: AsyncTask #2
  04-02 08:24:18.568: E/AndroidRuntime(2380): java.lang.RuntimeException: An error occured while executing doInBackground()
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at java.lang.Thread.run(Thread.java:841)
  04-02 08:24:18.568: E/AndroidRuntime(2380): Caused by: java.lang.NullPointerException
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at com.example.dvr.MainActivity$login.doInBackground(MainActivity.java:123)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at com.example.dvr.MainActivity$login.doInBackground(MainActivity.java:1)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  04-02 08:24:18.568: E/AndroidRuntime(2380):     ... 4 more
  04-02 08:24:19.868: E/WindowManager(2380): Activity com.example.dvr.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52e3cf88 V.E..... R......D 0,0-1026,288} that was originally added here
  04-02 08:24:19.868: E/WindowManager(2380): android.view.WindowLeaked: Activity com.example.dvr.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52e3cf88 V.E..... R......D 0,0-1026,288} that was originally added here
  04-02 08:24:19.868: E/WindowManager(2380):  at android.view.ViewRootImpl.&lt;init&gt;(ViewRootImpl.java:345)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.app.Dialog.show(Dialog.java:281)
  04-02 08:24:19.868: E/WindowManager(2380):  at com.example.dvr.MainActivity$login.onPreExecute(MainActivity.java:96)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.os.AsyncTask.execute(AsyncTask.java:534)
  04-02 08:24:19.868: E/WindowManager(2380):  at com.example.dvr.MainActivity$2.onClick(MainActivity.java:70)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.view.View.performClick(View.java:4240)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.view.View$PerformClick.run(View.java:17721)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.os.Handler.handleCallback(Handler.java:730)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.os.Handler.dispatchMessage(Handler.java:92)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.os.Looper.loop(Looper.java:137)
  04-02 08:24:19.868: E/WindowManager(2380):  at android.app.ActivityThread.main(ActivityThread.java:5103)
  04-02 08:24:19.868: E/WindowManager(2380):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-02 08:24:19.868: E/WindowManager(2380):  at java.lang.reflect.Method.invoke(Method.java:525)
  04-02 08:24:19.868: E/WindowManager(2380):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  04-02 08:24:19.868: E/WindowManager(2380):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  04-02 08:24:19.868: E/WindowManager(2380):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <ul>
  <li><strong>php code:</strong></li>
  </ul>
  
  <h1>1)db_config.php</h1>
  
  <pre><code>&lt;?php
  
  /*
   * All database connection variables
   */
  
  define('DB_USER', "********"); // db user
  define('DB_PASSWORD', "*********"); // db password (mention your db password here)
  define('DB_DATABASE', "databse"); // database name
  define('DB_SERVER', "localhost"); // db server
  ?&gt;
  </code></pre>
  
  <h1>2)adduser.php</h1>
  
  <pre><code> &lt;?php
  
      /*
       * Following code will create a new product row
       * All product details are read from HTTP Post Request
       */
  
      // array for JSON response
      $response = array();
  
      // check for required fields
      if (isset($_POST['user']) &amp;&amp; isset($_POST['password']) ) {
  
          $user = $_POST['user'];
          $password = $_POST['password'];
  
  
          // include db connect class
          require_once __DIR__ . '/db_connect.php';
  
          // connecting to db
          $db = new DB_CONNECT();
  
          // mysql inserting a new row
          $result = mysql_query("INSERT INTO users(user, password) VALUES('$user', '$password')");
  
          // check if row inserted or not
          if ($result) {
              // successfully inserted into database
              $response["success"] = 1;
              $response["message"] = "Product successfully created.";
  
              // echoing JSON response
              echo json_encode($response);
          } else {
              // failed to insert row
              $response["success"] = 0;
              $response["message"] = "Oops! An error occurred.";
  
              // echoing JSON response
              echo json_encode($response);
          }
      } else {
          // required field is missing
          $response["success"] = 0;
          $response["message"] = "Required field(s) is missing";
  
          // echoing JSON response
          echo json_encode($response);
      }
      ?&gt;
  </code></pre>
  
  <h1>3)login.php</h1>
  
  <pre><code>&lt;?php
  //array for JSON response
  $response = array();
  
  // check for required fields
  if (isset($_POST['user']) &amp;&amp; isset($_POST['password']) ) {
  
      $user = $_POST['user'];
      $password = $_POST['password'];
  
  
      // include db connect class
      require_once '/db_connect.php';
  
      // connecting to db
      $db = new DB_CONNECT();
  
      // mysql inserting a new row
      $sql = mysql_query("Select user, password from users where user='$user'")or die(mysql_error());
      $count_query = mysql_num_rows($query);
      if($sql){
       $response["success"] = 0;
          $response["message"] = "Wrong User Or Pass";
  
          // echoing JSON response
          echo json_encode($response);
      }
       else{
          $response["success"] = 1;
          $response["message"] = "correct Informations";
  
          // echoing JSON response
          echo json_encode($response);
      }
  }
  else {
      // required field is missing
      $response["success"] = 0;
      $response["message"] = "Required field(s) is missing";
  
      // echoing JSON response
      echo json_encode($response);
  }   
  ?&gt;
  </code></pre>
  
  <p><strong>the Error is in the login action how to fix this error i need it to work.</strong></p>
  
  <ul>
  <li><strong>android code:</strong></li>
  </ul>
  
  <h1>1) JSONParser.java</h1>
  
  <pre><code>package com.example.dvr;
  
  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.HttpGet;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.client.utils.URLEncodedUtils;
  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 = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      // function get json from url
      // by making HTTP POST or GET mehtod
      public JSONObject makeHttpRequest(String url, String method,
              List&lt;NameValuePair&gt; params) {
  
          // Making HTTP request
          try {
  
              // check for request method
              if(method == "POST"){
                  // request method is POST
                  // defaultHttpClient
                  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();
  
              }else if(method == "GET"){
                  // request method is GET
                  DefaultHttpClient httpClient = new DefaultHttpClient();
                  String paramString = URLEncodedUtils.format(params, "utf-8");
                  url += "?" + paramString;
                  HttpGet httpGet = new HttpGet(url);
  
                  HttpResponse httpResponse = httpClient.execute(httpGet);
                  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();
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  </code></pre>
  
  <h1>2)MainActivity.java</h1>
  
  <pre><code>package com.example.dvr;
  
  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.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
  
      // Progress Dialog
      private ProgressDialog pDialog;
  
      JSONParser jsonParser = new JSONParser();
      EditText username_edt;
      EditText pass_edt;
  
      // url to creaate new product
      private static String url_create_user = "http://10.0.3.2/android_connect/adduser.php";
      private static String url_login_user = "http://10.0.3.2/android_connect/login.php";
  
      // JSON Node names
      private static final String TAG_SUCCESS = "success";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Edit Text
          username_edt = (EditText) findViewById(R.id.editText1);
          pass_edt = (EditText) findViewById(R.id.editText2);
          // inputDesc = (EditText) findViewById(R.id.inputDesc);
  
          // Create button
          Button btnCreateProduct = (Button) findViewById(R.id.btnlogin);
  
          // button click event
          btnCreateProduct.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View view) {
                  // creating new product in background thread
                  new CreateNewProduct().execute();
              }
          });
  
          // Login button
          Button loginBtn = (Button) findViewById(R.id.btn_login);
  
          // button click event
          loginBtn.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View view) {
                  // creating new product in background thread
                  new login().execute();
  
                  Toast.makeText(getBaseContext(),
                          username_edt.toString() + pass_edt.toString(),
                          Toast.LENGTH_SHORT).show();
              }
          });
  
      }
  
      /**
       * Selecting Users
       * */
  
      class login extends AsyncTask&lt;String, String, String&gt; {
  
          /**
           * Before starting background thread Show Progress Dialog
           * */
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(MainActivity.this);
              pDialog.setMessage("Creating Product..");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(true);
              pDialog.show();
          }
  
          /**
           * Creating product
           * */
          protected String doInBackground(String... args) {
              String user = username_edt.getText().toString();
              String password = pass_edt.getText().toString();
              // String description = inputDesc.getText().toString();
  
              // Building Parameters
              List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("user", user));
              params.add(new BasicNameValuePair("password", password));
              // params.add(new BasicNameValuePair("description", description));
  
              // getting JSON Object
              // Note that create product url accepts POST method
              JSONObject json = jsonParser.makeHttpRequest(url_login_user,
                      "POST", params);
  
              // check log cat from response
          //  Log.d("Create Response", json.toString());
  
              // check for success tag
              try {
                  int success = json.getInt(TAG_SUCCESS);
  
                  if (success == 1) {
  
                      finish();
  
                  } else {
                      // failed
                  }
              } 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();
  
          }
  
      }
  
      /**
       * Background Async Task to Create new product
       * */
      class CreateNewProduct extends AsyncTask&lt;String, String, String&gt; {
  
          /**
           * Before starting background thread Show Progress Dialog
           * */
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(MainActivity.this);
              pDialog.setMessage("Creating Product..");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(true);
              pDialog.show();
          }
  
          /**
           * Creating product
           * */
          protected String doInBackground(String... args) {
              String user = username_edt.getText().toString();
              String password = pass_edt.getText().toString();
              // String description = inputDesc.getText().toString();
  
              // Building Parameters
              List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("user", user));
              params.add(new BasicNameValuePair("password", password));
              // params.add(new BasicNameValuePair("description", description));
  
              // getting JSON Object
              // Note that create product url accepts POST method
              JSONObject json = jsonParser.makeHttpRequest(url_create_user,
                      "POST", params);
  
              // check log cat from response
              Log.d("Create Response", json.toString());
  
              // check for success tag
              try {
                  int success = json.getInt(TAG_SUCCESS);
  
                  if (success == 1) {
  
                      finish();
  
                  } else {
                      // failed
                  }
              } 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();
          }
  
      }
  }
  </code></pre>