Tzen Szen Toh
Tzen Szen Toh

Reputation: 87

Display webpage in Android listview

I would like to get the data from here and display it in my app.

The list view is current empty when the activity is launched and I'm not exactly sure what I need to add, I'm quite stuck. Would really appreciate any help, thanks!
Here is the relevant code:

public class Details extends AppCompatActivity {

private static final String KEY_LINK = "link";

private ArrayAdapter<String> mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    new GetData().execute();
}

private class GetData extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... uri) {
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        // getting intent data
        Intent in = getIntent();

        // Get JSON values from previous intent
        String link = in.getStringExtra(KEY_LINK);
        String info = null;

        final String QUERY = link;

        Uri.Builder builder = new Uri.Builder();
        builder.scheme("https")
                .authority("www.minerva.shef.ac.uk")
                .appendPath("minerva")
                .appendPath("med")
                .appendPath("includes")
                .appendPath("inc_news_details.php")
                .appendQueryParameter("id", QUERY);


        try {

            URL url = new URL(builder.build().toString());

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            if(urlConnection.getResponseCode() == HttpsURLConnection.HTTP_OK){
                Log.d("Hello,", "Spongebob!");
                // Do normal input or output stream reading
            }
            else {
                 // See documentation for more info on response handling
            }

            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();

            reader = new BufferedReader(new InputStreamReader(inputStream));
            info = buffer.toString();

            List<String> details = new ArrayList<String>(Arrays.asList(info));

            mAdapter = new ArrayAdapter<String>(getApplication(), R.layout.list_item, R.id.link, details);


        } catch (IOException e) {
            Log.e("Hi", "This is Patrick");

            return null;
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(mAdapter);


    }
}

}

Upvotes: 0

Views: 65

Answers (1)

Arpit Ratan
Arpit Ratan

Reputation: 3026

To achieve this you have to use web view instead of loading the url using HTTPURLConnection. This is because this is not a standard rest based api which returns the data in xml and json format.

In your activity_details.xml file. Remove list view and add the following view.

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
/>

And then add following line of code in your activity.

WebView browser = (WebView) findViewById(R.id.webview);
browser.loadUrl("https://www.minerva.shef.ac.uk/minerva/med/includes/inc_news_details.php?id=" + mID);

If still you insist to do this using ListView then you will have to parse the data yourself putting hacks which is not a standard way of doing it.

Upvotes: 1

Related Questions