如何将部分维基百科内容检索到 Android 应用程序中?

发布于 2024-11-09 14:55:57 字数 678 浏览 0 评论 0原文

基本上,我想从维基百科检索内容。 但我想直接在我的 Android 应用程序中显示它。 不会立即重定向到互联网浏览器,而是首先在我的应用程序中显示它。

目前,我设法请求维基百科 API 并通过使用 http://en.wikipedia.org/w/api.php?action=parse&prop=text&format=xml&page=Bla_Bla_Bla。 因为我解析数据,所以我将使用WebView在Android中渲染。就渲染成功了。但仅限于那些不受保护的文章...

如果它受到保护,例如 Mona Lisa,输出未在 Android WebView 中正确呈现。

我想知道是否有人尝试检索维基百科内容并将其显示在您的 Android 应用程序中,轻松且美观?

谢谢 :)

Basically, I want to retrieve content from wikipedia.
But I want to display it inside my Android Apps directly.
Not immidiately redirect to the internet browser, but to display it inside my apps first.

Currently, I manage to request the Wikipedia API and get only the main content by using http://en.wikipedia.org/w/api.php?action=parse&prop=text&format=xml&page=Bla_Bla_Bla.
and because I parse the data, I will use WebView to render in the Android. It successfully rendered. But only to those unprotected article...

If it is protected such as Mona Lisa, the output was not rendered properly in the WebView Android.

I want to know has anybody try to retrieve a wikipedia content and display it in your android apps, easily and beautifully?

Thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬花如无物 2024-11-16 14:55:57

我已经设法找到答案。我想我把这件事搞得太复杂了。
我们实际上可以完美地检索内容,而无需调用 mediawiki API。
维基百科已经提供了移动界面。

我只需要使用 WebView 来加载 http://en.m.wikipedia.org/wiki/ 并在网址末尾添加主题。
例如蒙娜丽莎网站: https://en.m.wikipedia.org/wiki/Mona_Lisa 参考文献


http://en.wikipedia.org/wiki/Help:Mobile_access#Official_online_version

I have managed to find an answer. I think I over complicated this thing.
We can actually retrieve the content perfectly without calling the mediawiki API.
Wikipedia already provides a mobile interface.

I just need to use a WebView to load http://en.m.wikipedia.org/wiki/ and add the topic at the end of the url.
For example the Mona Lisa site: https://en.m.wikipedia.org/wiki/Mona_Lisa

References:
http://en.wikipedia.org/wiki/Help:Mobile_access#Official_online_version

蹲在坟头点根烟 2024-11-16 14:55:57

我可能会检索 api 调用的 json 版本(在请求 uri 中使用 format=json )。您已经成功地实现了数据检索(我猜是使用 HttpPost 或 HttpGet),因此现在的问题只是检索正确的数据以便在您的应用程序中使用。

我目前正在编写一个从服务器检索 JSON 的应用程序,并且获取内容非常容易。只需实例化一个 JSONObject 并向其提供来自服务器的 json 结果,然后使用对象中的 get 方法检索数据即可。

简单的例子:

JSONObject jsonObject = new JSONObject(responseTextFromServer);
String query = jsonObject.getString("query");
// and so on...

I would probable retrieve the json version of the api call (with format=json in the request uri). You have managed to get the retrieval of the data (with an HttpPost or HttpGet, I guess) working, so now it's only a question of retrieving the correct data for use in your application.

I'm currently writing an application that retrieves JSON from a server, and it's really easy to get the content. Just instantiate a JSONObject and feed it the json result from the server, then retrieve the data with the get methods in the object.

Simple example:

JSONObject jsonObject = new JSONObject(responseTextFromServer);
String query = jsonObject.getString("query");
// and so on...
何必那么矫情 2024-11-16 14:55:57
this is main activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView_phones"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>


this is second activity xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".wikipedia_search">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
this is main activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView_phones"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>


this is second activity xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".wikipedia_search">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
Hello爱情风 2024-11-16 14:55:57
// this is main activity code. I have added list of cars and when clicked it displays the wikipedia about it

package com.example.google_phones;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView listView_google_phones;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView_google_phones= (ListView)findViewById(R.id.listView_phones);
        final ArrayList<String> arrayList= new ArrayList<>();
        arrayList.add("Google Pixel");
        arrayList.add("Google Pixel XL");
        arrayList.add("Google Pixel 2");
        arrayList.add("Google Pixel 2XL");
        arrayList.add("Google Pixel 3");
        arrayList.add("Pixel_3");



         ArrayAdapter arrayAdapter= new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,arrayList);
        listView_google_phones.setAdapter(arrayAdapter);

        listView_google_phones.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this,wikipedia_search.class);
                intent.putExtra("Phone_name", listView_google_phones.getItemAtPosition(position).toString());
                startActivity(intent);
            }
        });
    }
}

// this is second activity code when user clicks on any one of the phones

package com.example.google_phones;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class wikipedia_search extends AppCompatActivity {
 WebView webView_wiki;
 private String search_string;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wikipedia_search);

        webView_wiki= (WebView)findViewById(R.id.webView);
        webView_wiki.setWebViewClient(new WebViewClient());
        Bundle bundle=getIntent().getExtras();
        if(bundle!=null){
            webView_wiki.loadUrl("http://en.m.wikipedia.org/wiki/"+bundle.getString("Phone_name"));
        }

    }
}
// this is main activity code. I have added list of cars and when clicked it displays the wikipedia about it

package com.example.google_phones;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView listView_google_phones;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView_google_phones= (ListView)findViewById(R.id.listView_phones);
        final ArrayList<String> arrayList= new ArrayList<>();
        arrayList.add("Google Pixel");
        arrayList.add("Google Pixel XL");
        arrayList.add("Google Pixel 2");
        arrayList.add("Google Pixel 2XL");
        arrayList.add("Google Pixel 3");
        arrayList.add("Pixel_3");



         ArrayAdapter arrayAdapter= new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,arrayList);
        listView_google_phones.setAdapter(arrayAdapter);

        listView_google_phones.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this,wikipedia_search.class);
                intent.putExtra("Phone_name", listView_google_phones.getItemAtPosition(position).toString());
                startActivity(intent);
            }
        });
    }
}

// this is second activity code when user clicks on any one of the phones

package com.example.google_phones;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class wikipedia_search extends AppCompatActivity {
 WebView webView_wiki;
 private String search_string;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wikipedia_search);

        webView_wiki= (WebView)findViewById(R.id.webView);
        webView_wiki.setWebViewClient(new WebViewClient());
        Bundle bundle=getIntent().getExtras();
        if(bundle!=null){
            webView_wiki.loadUrl("http://en.m.wikipedia.org/wiki/"+bundle.getString("Phone_name"));
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文