8

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

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
Yeo
  • 11,416
  • 6
  • 63
  • 90

4 Answers4

3

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

Carmen
  • 6,177
  • 1
  • 35
  • 40
Yeo
  • 11,416
  • 6
  • 63
  • 90
0

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...
Patrick
  • 1,044
  • 1
  • 10
  • 18
  • You're right. But isn't it the same output either XML or JSON. btw, I've manage to extract the data. – Yeo May 22 '11 at 17:51
  • Given that the question is differently phrased from when I originally answered: best bet is to filter out all html formatting in the reply from Wikipedia. It looks like the reply has all the formatting you'd see on the regular page, which could become an issue when the data is parsed and displayed. – Patrick May 22 '11 at 18:41
  • Thank you so much for helping me, I have manage to solve it =D – Yeo May 28 '11 at 19:05
0
// 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
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>