I am trying to get data from online database(MySql) through json and want to display it as a ListView. What I've done so far :
Downloader.java
public class Downloader extends AsyncTask<Void,Integer,String> { Context c; String address; ListView lv; ProgressDialog pd; public Downloader(Context c, String address, ListView lv) { this.c = c; this.address = address; this.lv = lv; } //B4 JOB STARTS @Override protected void onPreExecute() { super.onPreExecute(); pd=new ProgressDialog(c); pd.setTitle("Fetch Data"); pd.setMessage("Fetching Data...Please wait"); pd.show(); } @Override protected String doInBackground(Void... params) { String data=downloadData(); return data; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); pd.dismiss();; if(s != null) { Parser p=new Parser(c,s,lv); p.execute(); }else { Toast.makeText(c,"Unable to download data",Toast.LENGTH_SHORT).show(); } } private String downloadData() { //connect and get a stream InputStream is=null; String line =null; try { URL url=new URL(address); HttpURLConnection con= (HttpURLConnection) url.openConnection(); is=new BufferedInputStream(con.getInputStream()); BufferedReader br=new BufferedReader(new InputStreamReader(is)); StringBuffer sb=new StringBuffer(); if(br != null) { while ((line=br.readLine()) != null) { sb.append(line+"n"); } }else { return null; } return sb.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } }
MainActivity.java
public class MainActivity extends Activity { String url="http://bookvilla.esy.es/book.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView lv= (ListView) findViewById(R.id.lv); final Downloader d=new Downloader(this,url,lv); d.execute(); } } when i run the app the app.. i get a toast message saying "unable to download data" which is in the 35th line of Downloader.java class. So i am guessing that the data i am getting from the database is null ...but why is it so ....
$con = mysqli_connect($host,$u_name,$pwd,$db) or die('Unable to connect'); if(mysqli_connect_error($con)) { echo "Failed to connect to database".mysqli_connect_error();
}
$query = mysqli_query($con,"SELECT * FROM playerstb"); if($query) {
while($row=mysqli_fetch_array($query)) { $flag[] = $row;
} print(json_encode($flag)); } mysqli_close($con);
above is my php file


private String downloadData()even called? i don't think so. i think you forgot to override thepublic void execute()Method