title: Connecting android to mySQL using php and JSON
body: <p>I'm trying to create an android application that connects to mySQL database through php and JSON.
  THis is my php file on the server:</p>
  
  <pre><code>&lt;?php
  $host="my_host";
  $username="my_name";
  $password="my_password";
  $db_name="my_db_name";
  
  $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  $sql = "SELECT    UserID, DisplayName
  FROM      User
  WHERE     (UserName LIKE '%it%') OR (DisplayName LIKE '%it%')"; 
  $result = mysql_query($sql);
  $json = array();
  
  if(mysql_num_rows($result)){
  while($row=mysql_fetch_assoc($result)){
  $json['UserRes'][]=$row;
  }
  }
  mysql_close($con);
  echo json_encode($json); 
  ?&gt; 
  </code></pre>
  
  <p>And this is my JSON java class code:</p>
  
  <pre><code>public class JSONClass extends AsyncTask&lt;String,Void,String&gt;{
  
  public HashMap&lt;String, String&gt; tbl = new HashMap&lt;String, String&gt;();
  private Context context;
  
  public JSONClass(Context context) {
      this.context = context;
  }
  
  @Override
  protected String doInBackground(String... arg0) {
      try{
          String link = "some url";
          URL url = new URL(link);
          HttpClient client = new DefaultHttpClient();
          HttpGet request = new HttpGet();
          request.setURI(new URI(link));
          HttpResponse response = client.execute(request);
          BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
          StringBuffer sb = new StringBuffer("");
          String line="";
          while ((line = in.readLine()) != null) {
              sb.append(line);
              break;
          }
          in.close();
          return sb.toString();
      }catch(Exception e){
          return new String("Exception: " + e.getMessage());
      }
  }
  }
  </code></pre>
  
  <p>I'm new with this and just want to get the basics and work from there. The php file should return one row as an answer, but as far as I can figure it out, the function falls in the try - catch part. I know that the  result should come as HashMap. Can someone please tell me what I'm doing wrong or give me a tip to get the result? Thank you in advance!</p>