基本上,我想从维基百科检索内容。但我想直接在我的Android Apps中显示它。不是立即重定向到Internet浏览器,而是先在我的应用程序中显示它。
[目前,我设法通过http://en.wikipedia.org/w/api.php?action=parse&prop=text&format=xml&page=Bla_Bla_Bla请求Wikipedia API并仅获取主要内容。并且由于我解析了数据,因此我将使用WebView在Android中进行渲染。成功渲染。但仅限于那些不受保护的文章...
如果受保护,例如Mona Lisa,则输出未在WebView Android中正确呈现。
我想知道是否有人尝试检索维基百科内容并将其轻松轻松地显示在您的android应用中?
谢谢:)
我设法找到了答案。我想我把事情复杂化了。实际上,我们可以完美地检索内容,而无需调用mediawiki API。因为他们已经提供了移动界面。
我只需要加载http://en.m.wikipedia.org/wiki/并在后面添加主题。然后使用WebView对其进行查看。显示完美尼斯。 :)
参考:http://en.wikipedia.org/wiki/Help:Mobile_access#Official_online_version
我可能会检索api调用的json版本(请求uri中的format = json)。您已经设法使数据的检索工作正常(我想是使用HttpPost或HttpGet),所以现在这只是检索在应用程序中使用的正确数据的问题。
我当前正在编写一个从服务器检索JSON的应用程序,并且很容易获得内容。只需实例化一个JSONObject并将其从服务器中获取json结果,然后使用该对象中的get方法检索数据。
简单示例:
JSONObject jsonObject = new JSONObject(responseTextFromServer);
String query = jsonObject.getString("query");
// and so on...
// this is main activity code. I had an android assignment in college where I searched google phone on wikipedia and displayed it in webview
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
<?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>