Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $database = "bmb";
- $conn = new mysqli($servername, $username, $password, $database);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $heroes = array();
- $sql = "SELECT image FROM business"
- //creating an statment with the query
- $stmt = $conn->prepare($sql);
- //executing that statment
- $stmt->execute();
- //binding results for that statment
- $stmt->bind_result($id, $name);
- //looping through all the records
- while($stmt->fetch()){
- //pushing fetched data in an array
- $temp = [
- 'id'=>$id,
- 'image'=>$name
- ];
- //pushing the array inside the hero array
- array_push($heroes, $temp);
- }
- //displaying the data in json format
- echo json_encode($heroes);
- ?>
- and my android code is:
- class GetJSON extends AsyncTask<Void, Void, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- // Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
- try {
- loadIntoListView(s);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected String doInBackground(Void... voids) {
- try {
- URL url = new URL(urlWebService);
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- StringBuilder sb = new StringBuilder();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String json;
- while ((json = bufferedReader.readLine()) != null) {
- sb.append(json + "n");
- }
- return sb.toString().trim();
- } catch (Exception e) {
- return null;
- }
- }
- }
- GetJSON getJSON = new GetJSON();
- getJSON.execute();
- public String[] loadIntoListView(String json) throws JSONException {
- getJSON("http://192.168.43.88/ionic_side/getdata.php");
- JSONArray jsonArray = new JSONArray(json);
- ArrayList<String> heroes = new ArrayList();
- for (int i = 0; i < 4; i++)
- {
- try{
- JSONObject obj = jsonArray.getJSONObject(i);
- heroes[i] = "http://192.168.43.88/android/images/"+obj.getString("image");
- }catch(JSONException ignore_errors){
- }
- }
- return heroes.toArray(new String[0]);
- } where i called my function:
- String[] items= new String[30];
- if (ImageListFragment.this.getArguments().getInt("type") == 1){
- String s=null;
- try {
- items= loadIntoListView(s);
- } catch (JSONException e) {
- e.printStackTrace();
- } 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