简体   繁体   English

如何获取网站内容并将其放入我们想要的Android应用程序布局?

[英]How to fetch website content and put it in the way we want into the android app layout?

Its weird kind of question being asked from me, but curiosity is the best teacher and I eagerly wants to know and develop such kind of android app. 它问我很奇怪的问题,但好奇心是最好的老师,我急切地想知道并开发这种类型的Android应用程序。

My background in the android app development is not that great but right now I am using WebView for an website which is responsive its working well but its taking too much time to load and not providing that much convenience to the user. 我在Android 应用程序开发方面的 背景 并不是那么好,但是现在我正在使用WebView作为响应其工作良好的网站,但是它花费了太多时间来加载并且没有为用户提供那么多便利。 Here's the code that I have: 这是我的代码:

MainActivity 主要活动

package com.testWebview.deep;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView webview;
    private ProgressDialog progress;

    public void runTask(){
        ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connec!=null && (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) || (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED)){
            webview = (WebView)findViewById(R.id.MainWebView);
            WebSettings webSettings = webview.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setLoadsImagesAutomatically(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setDomStorageEnabled(true);
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webSettings.setAllowFileAccess(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setUseWideViewPort(true);
            loadingC();
            webview.setWebViewClient(new WebViewClient(){

                @Override
                public void onPageStarted(WebView view, String url,Bitmap favicon){
                    if(!progress.isShowing()){
                        loadingC();
                    }

                }
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    if(progress.isShowing()){progress.dismiss();}
                    checkConnection();
                    webview.loadUrl("file:///android_asset/error.html");
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    if(progress.isShowing()) {
                        progress.dismiss();
                    }
                }
            });
            webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            webview.setScrollbarFadingEnabled(false);
            webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            webview.loadUrl("http://www.bharatcoupons.com/");
        }
        else{
            checkConnection();
        }
    }

    public void checkConnection(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Error");
        builder.setMessage("Please make sure your connected to internet!");
        builder.setCancelable(false);
        builder.setPositiveButton("retry", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                runTask();
            }

        });
        builder.show();
    }

    public void loadingC(){
        progress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        runTask();
    }

    @Override
    public void onBackPressed(){
        if(webview.canGoBack()){
            webview.goBack();
        }else{
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class MyAppWebViewClient extends WebViewClient{

    }
}

MyAppWebViewClient MyAppWebViewClient

package com.testWebview.deep;

import android.content.Intent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.Uri;

public class MyAppWebViewClient extends WebViewClient{

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        if(Uri.parse(url).getHost().endsWith("bharatcoupons.com")){
            return false;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }

}

The above code working fine with webview but its kind of slow and not professional way of developing an android app. 上面的代码与webview一起工作正常,但它是一种缓慢而不专业的开发Android应用程序的方式。 Now what I want to do is fetch the website content and put it into the app frame that I want. 现在我想要做的是获取网站内容并将其放入我想要的应用程序框架中。 I will show you an example that will elaborate you all that I wanted to do. 我将向您展示一个例子,它将详细说明您想要做的所有事情。

Suppose my website is like this : 假设我的网站是这样的: 一个网站

What kind of approach should I follow or methods, tricks to make my app like this: 我应该遵循什么样的方法或方法,使我的应用程序像这样的技巧 在此输入图像描述

As per my knowledge its not an webview showing directly the website content into the app. 据我所知,它不是一个webview直接显示应用程序中的网站内容。 On swapping left or right it will show you the next item. 在向左或向右交换时,它将显示下一个项目。 The whole website into this app but not using WebView 整个网站进入这个应用程序但不使用WebView

They have made such framework that is fetching the content from website and putting it into the app. 他们制作了这样的框架,即从网站获取内容并将其放入应用程序。 and I want to do that. 而我想这样做。

I don't want code but from where should I start, What kind of approach or technique should I use to achieve that. 我不想要代码,但是我应该从哪里开始,我应该使用什么样的方法或技术来实现它。

The reference website and app I am taking for educational purpose only no other intention behind it 我参与教育目的的参考网站和应用程序背后没有其他意图

Thanks in advance! 提前致谢!

Approach 1: 方法1:

Use a responsive website that will automatically fit the mobile/tab screen. 使用自动适合移动/标签屏幕的自适应网站。

Approach 2: 方法2:

Parse the web documents like images and text then show it in ImageView or other UI contents. 解析Web图像和文本等文档,然后在ImageView或其他UI内容中显示它。

For HTML parsing you can use a 3rd party library like JSOUP 对于HTML解析,您可以使用第三方库,如JSOUP

Best Approach: 最佳方法:

Now a days almost all companies/online services use an API for communication between the web and other platforms. 现在几乎所有的公司/在线服务都使用API​​来进行Web和其他平台之间的通信。 It keeps all data aligned and flexible since all information are coming from a same source. 它使所有数据保持一致和灵活,因为所有信息都来自同一来源。 Create a RESTful API for your web service. 为您的Web服务创建RESTful API。 Make call to the API and fetch data in a JSON format. 调用API并以JSON格式获取数据。 Later parse the info and show it it in a view. 稍后解析信息并在视图中显示它。

eg to get the infromation of a specific product, 例如,获取特定产品的信息,

http://your_address/shopping/v1/get_product_info HTTP:// your_address /购物/ V1 / get_product_info

在此输入图像描述

The result in JSON format could be something like this, JSON格式的结果可能是这样的,

{
    "error": false,
    "name": "ETC Solid Men's Polo Neck -Tshirt",
    "description": "Your pruduct description",
    "images": [
        {
            "preview1": "http://yoururl.com/images/preview1"
        },
        {
            "preview2": "http://yoururl.com/images/preview2"
        },
        {
            "preview3": "http://yoururl.com/images/preview3"
        },
        {
            "preview4": "http://yoururl.com/images/preview4"
        }
    ]
}

A good time/work/results approach is to "wrap" your website within a simple Cordova application. 一个好的时间/工作/结果方法是将您的网站“包装”在一个简单的Cordova应用程序中。 So you should start learning Cordova , then add a responsive/custom css code for the mobile world on your site (if doesn't exists yet) and then fixing everything that doesn't works correctly on the phone (js/css). 因此,您应该开始学习Cordova ,然后为您的网站上的移动世界添加响应/自定义css代码(如果尚不存在),然后修复在手机上无法正常工作的所有内容(js / css)。

If this website have an Api you can do it with the api if it has json data you can connect via json and in the worst case you can parse its html into xml or json or straight to the data objects you need. 如果这个网站有一个Api,你可以使用api,如果它有json数据,你可以通过json连接,在最坏的情况下,你可以解析它的HTML到xml或json或直接到你需要的数据对象。 The reason why this approach is less recommended is because if they change something in the site that might brake your interface immediately. 之所以不推荐这种方法的原因是因为如果他们在站点中改变某些可能会立即制动您的界面的东西。

Big sites usually have an api check: Amazon api Facebook api Soundcloud api 大型网站通常都有api支票:Amazon api Facebook api Soundcloud api

Etc 等等

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何将aspx页面内容解析到Android应用程序? - How can we parse a aspx page content to an Android app? 如何将GLSurfaceView放在android上的布局中 - How to put GLSurfaceView inside a layout on android 如何将按钮放在android java布局的右侧 - how to put the button on the right of the android java layout 如何对 android 中的相关布局产生涟漪效应? - How to put ripple effect on relative layout in android? 如何在android中获取或获取或下载任何网站的网站图标或网站图标 - how to get or fetch or download favicon or website icon of any website in android 适用于Android的文本冒险应用程序-如何根据整数绘制XML布局的不同内容 - Text adventure app for Android - how to draw different content of XML layout depending on the an integer Android studio 无法使用 Wrap 内容时,如何为不同屏幕尺寸的多种布局配置应用程序? - Android studio How to configure app for multiple layout for deferent screens sizes when can't use Wrap content? 如何搜索其他网站的内容并检索结果并放置在我们的Android应用程序中? - How to search content of other website and retrieve the result and place in our Android app? WebView Android应用程序-如何阅读网站内容 - WebView Android application - how to read website content 有没有办法将正在运行的android应用作为android设备墙纸? - Is there a way to put a running android app as the android device wallpaper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM