Note that there are some explanatory texts on larger screens.

plurals
  1. POConnect MySQL database from Android
    text
    copied!<p>well this is the code snippet i use to access the getUser.php to retrive user details from a MySQL database in my application:</p> <pre><code>String result = ""; //the year data to send ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); nameValuePairs.add(new BasicNameValuePair("uid","demo")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://192.xxx.xx.xxx/getUser.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ InputStream is = null; 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(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } //parse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i&lt;jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("fname")+ ", sex: "+json_data.getInt("sex")+ ", birthyear: "+json_data.getInt("dob") ); } } catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } } </code></pre> <p>This snippet is taken from <a href="http://helloandroid.com" rel="nofollow">http://helloandroid.com</a></p> <p>Everything is configured fine: the MySQL Db, IIS with FASTCGi, PHP tools and drivers. even the script below when called from browser with url: <a href="http://192.xxx.xx.x.xxx/getUser.php?uid=demo" rel="nofollow">http://192.xxx.xx.x.xxx/getUser.php?uid=demo</a> works fine, But returns error in android with <strong>java.lang.NullPointerException</strong> and <strong>org.json.JSONEXCEPTION: End of input at character 0</strong></p> <pre><code>&lt;?php mysql_connect("myhost","username","pwd"); mysql_select_db("mydb"); $q=mysql_query("SELECT * FROM userinfo WHERE uid ='".$_REQUEST['uid']."'"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close(); ?&gt; </code></pre> <p>Can anybody help in this section?</p> <p>Regards, Mistry Hardik</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload