首页 > 解决方案 > 如何使用 ReactNative WebView 组件在我的 Android 应用程序中显示网页的特定部分?

问题描述

我想在我的 android 应用程序中使用 ReactNative webView 组件并只显示网页的特定部分。例如我想在下图中显示红色矩形指定的内容:

一个github用户

我不需要网页的其他部分。我可以通过以下代码显示整个页面:

<WebView
  source={{ uri: https://github.com/TrySound }}
  style={{ marginTop: 20 }}
/>

但不知道如何只显示红色矩形中的内容。有人可以帮我解决这个问题吗?

标签: javascriptandroidreact-nativewebviewreact-native-android

解决方案


WebView 不适用于您的情况。Github 确实有一个 RESTful API。您可以对用户名发出 GET 请求。您将收到一个 JSON 对象,并且您想要的信息将在 bio 下。

https://developer.github.com/v3/users/#get-a-single-user

您可以使用 fetch api 获取您的简历

function getBio(){
  return fetch('https://api.github.com/users/trysound')
  .then(function(response) { return response.json();})
  .then(function(data) {return data.bio;});
}

并访问 api 请求的结果,您可以执行以下操作

getBio().then(function(bio){
    // insert your code here to handle the string for your bio on github
    console.log(bio);
});

推荐阅读