在Android列表视图中显示网页

时间:2016-06-12 11:44:39

标签: android httprequest

我想从here获取数据并将其显示在我的应用中。

当活动启动时,列表视图是当前空的,我不确定我需要添加什么,我很困惑。非常感谢任何帮助,谢谢! 以下是相关代码:

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);


    }
}

}

1 个答案:

答案 0 :(得分:1)

要实现此目的,您必须使用Web视图,而不是使用HTTPURLConnection加载URL。这是因为这不是基于休息的标准api,它以xml和json格式返回数据。

在您的activity_details.xml文件中。删除列表视图并添加以下视图。

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

然后在您的活动中添加以下代码行。

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

如果仍然坚持使用ListView执行此操作,那么您将不得不这样做   解析数据自己把黑客,这不是一种标准的方式   这样做。