Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $database = "bmb";
  7.  
  8.  
  9.  
  10. $conn = new mysqli($servername, $username, $password, $database);
  11.  
  12.  
  13. if ($conn->connect_error) {
  14. die("Connection failed: " . $conn->connect_error);
  15. }
  16.  
  17.  
  18. $heroes = array();
  19.  
  20.  
  21. $sql = "SELECT image FROM business"
  22.  
  23. //creating an statment with the query
  24. $stmt = $conn->prepare($sql);
  25.  
  26. //executing that statment
  27. $stmt->execute();
  28.  
  29. //binding results for that statment
  30. $stmt->bind_result($id, $name);
  31.  
  32. //looping through all the records
  33. while($stmt->fetch()){
  34.  
  35. //pushing fetched data in an array
  36. $temp = [
  37. 'id'=>$id,
  38. 'image'=>$name
  39. ];
  40.  
  41. //pushing the array inside the hero array
  42. array_push($heroes, $temp);
  43. }
  44.  
  45. //displaying the data in json format
  46. echo json_encode($heroes);
  47. ?>
  48.  
  49. and my android code is:
  50.  
  51. class GetJSON extends AsyncTask<Void, Void, String> {
  52.  
  53. @Override
  54. protected void onPreExecute() {
  55. super.onPreExecute();
  56. }
  57.  
  58.  
  59. @Override
  60. protected void onPostExecute(String s) {
  61. super.onPostExecute(s);
  62. // Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  63. try {
  64. loadIntoListView(s);
  65. } catch (JSONException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69.  
  70. @Override
  71. protected String doInBackground(Void... voids) {
  72. try {
  73. URL url = new URL(urlWebService);
  74. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  75. StringBuilder sb = new StringBuilder();
  76. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  77. String json;
  78. while ((json = bufferedReader.readLine()) != null) {
  79. sb.append(json + "n");
  80. }
  81. return sb.toString().trim();
  82. } catch (Exception e) {
  83. return null;
  84. }
  85. }
  86. }
  87. GetJSON getJSON = new GetJSON();
  88. getJSON.execute();
  89.  
  90. public String[] loadIntoListView(String json) throws JSONException {
  91. getJSON("http://192.168.43.88/ionic_side/getdata.php");
  92.  
  93. JSONArray jsonArray = new JSONArray(json);
  94. ArrayList<String> heroes = new ArrayList();
  95. for (int i = 0; i < 4; i++)
  96. {
  97. try{
  98.  
  99. JSONObject obj = jsonArray.getJSONObject(i);
  100. heroes[i] = "http://192.168.43.88/android/images/"+obj.getString("image");
  101.  
  102.  
  103. }catch(JSONException ignore_errors){
  104.  
  105. }
  106. }
  107. return heroes.toArray(new String[0]);
  108. } where i called my function:
  109.  
  110. String[] items= new String[30];
  111. if (ImageListFragment.this.getArguments().getInt("type") == 1){
  112. String s=null;
  113. try {
  114. items= loadIntoListView(s);
  115.  
  116. } catch (JSONException e) {
  117. e.printStackTrace();
  118. } i am expecting "items" array to be string array containing data for example items[]={"http://.....x.png", "http://sddffggg.jpg"} but i am getting errors please how can i do it or please help me correct my errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement