开发者

How to retrieve parts of Wikipedia content into Android App?

开发者 https://www.devze.com 2023-03-07 09:48 出处:网络
Basically, I want to retrieve content from wikipedia. But I want to display it inside my Android Apps directly.

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 kno开发者_运维技巧w has anybody try to retrieve a wikipedia content and display it in your android apps, easily and beautifully?

Thank you :)


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


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...


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

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消