39 thoughts on “Android Simple MySQL and PHP – Fetch data to List

  1. import android.os.StrictMode;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    import org.json.JSONArray;
    import org.json.JSONObject;

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.*;

    public class MainActivity extends AppCompatActivity {

    ListView lv;
    ArrayAdapter<String> adapter;
    String address = "http://localhost:8080/Android/v1/getData.php&quot;;
    InputStream is = null;
    String line = null;

    String result = null;
    String[] data;
    JSONObject jo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.listview);

    //ALLOW NETWORK IN MAIN THREAD
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

    //RETRIVE
    getData();

    //ADAPTER
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
    lv.setAdapter(adapter);

    }

    private void getData() {
    try {
    URL url = new URL(address);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    con.setRequestMethod("GET");

    is = new BufferedInputStream(con.getInputStream());
    } catch (Exception e) {
    e.printStackTrace();
    }

    //READ IS CONTENT INTO A STRING
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    while ((line = br.readLine()) != null) {
    sb.append(line + "n");
    }
    is.close();
    result = sb.toString();
    } catch (Exception e) {
    e.printStackTrace();

    //PARSE JSON DATA
    try {
    JSONArray ja = new JSONArray(result);

    data = new String[ja.length()];

    for (int i = 0; i < ja.length(); i++) {
    jo = ja.getJSONObject(i);
    data[i] = jo.getString("Name");
    }

    } catch (Exception e1) {
    e1.printStackTrace();
    }
    }
    }
    }//can anyone solve this

  2. Thx for this TuT Sir.
    I did all you show me on this nice Video.
    But in my App it shows me nothing.
    if i open the Test.php ( so i named my php ) on the normaly webbrowser its shows me the results i want to see:

    [{"0":"Dies ist ein test","tourette":"Dies ist ein test"}]

    but on the App is nothing, i checked the Video 3 times, and can't see any mistakes. HM…

    Android Studio has some problem with this:

    Yours is:

    String[] data;

    But my Android Studio want it this way:

    String[] data = new String[0];

    adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, data);
    lv.setAdapter(adapter);

    Maybe this is the reason? if yes, how to solve this Sir ? thx

Leave a Reply

Your email address will not be published. Required fields are marked *