【问题标题】:Pulling data from the web with Android Java [duplicate]使用 Android Java 从 Web 中提取数据 [重复]
【发布时间】:2014-07-18 15:50:31
【问题描述】:

所以我对 Java 和特别是 android 编程还很陌生。我正在尝试创建一个从金融网站提取数据的应用程序(如果它更容易的话,可能带有 API)。

我尝试的第一步是从网站上提取任何文本。我目前正在使用 .txt URL 进行练习,这是我目前的代码:

package com.example.datatesting;

import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        TextView tv = (TextView) findViewById(R.id.textView1);


        String myString = null;

        try{

            URL myURL = new URL("http://www.something.com/readme.txt");

            URLConnection connect= myURL.openConnection();

            InputStream ins = connect.getInputStream();
            BufferedInputStream buff = new BufferedInputStream(ins);


            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while( (current=buff.read()) !=-1){
                baf.append( (byte) current);
            }
            myString = new String(baf.toByteArray());
            tv.setText("hello1");
        }
        catch(Exception e){
            myString = e.getMessage();
            tv.setText("hello2");
        } 

    }
}

代码打印“hello2”。我不太确定出了什么问题或如何解决问题,因此 try 块可以正常工作。

我也在清单中添加了这个:

<uses-permission
    android:name="android.permission.INTERNET" />

我没有收到应用程序允许互联网访问的提示,是自动的吗?

感谢您的帮助和指导。

************编辑更新:我添加了 cmets 以指示混乱的区域

public class MainActivity extends Activity {

    private TextView tv;
    private String myString = null;

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

                tv = (TextView) findViewById(R.id.textView1);

                //I'm not sure what to put into execute(...) so I added this here, but this requires
                //a try catch block which would go back to my original issue...
                URL myURL = new URL("http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
                new DataExtract().execute(myURL);

        }


        private class DataExtract extends AsyncTask<URL, Void, Void>{

            protected Void doInBackground(URL...urls){ //this needs a return type but I'm not returning anything 
                try{
                    URL myURL = new URL("http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");

                    URLConnection ucon = myURL.openConnection();

                    InputStream is = ucon.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);


                    ByteArrayBuffer baf = new ByteArrayBuffer(50);
                    int current = 0;
                    while( (current=bis.read()) !=-1){
                        baf.append( (byte) current);
                    }
                    myString = new String(baf.toByteArray());
                    tv.setText("hello1");

                }
                catch(Exception e){
                    myString = e.getMessage();
                    tv.setText("hello2");

                }
            }

            protected void onPostExecute(Void result){ //is this an acceptable param?
                tv.setText(myString);
            }


        }
}

【问题讨论】:

    标签: java android web-scraping data-mining


    【解决方案1】:

    onCreate 在应用程序的主(或 UI)线程中执行。

    如果你尝试在 UI 线程中进行网络操作,你会得到一个NetworkOnMainThreadException

    解决这个问题的一种方法是使用AsyncTask,就像答案here.一样

    另请注意,您无法触摸来自doInBackground 的视图。您可以从onPostExecute 将值设置为TextView

    【讨论】:

    • 我的 android 基础围绕着使用 onCreate 和 setContentView。我目前正在尝试使用 AsyncTask。我创建了一个新的私有类,我无法找到引用 TextView 的方法。为了清楚起见,我应该将 try catch 代码移到扩展 AsyncTask ? 的新类中
    • 您的文本视图可以是 MainActivity 的私有成员。您的类(扩展 AsyncTask)可以是 MainActivity 的私有类成员。示例github.com/twitter-university/LearningAndroidYamba/blob/master/…
    • 感谢您的帮助!我对整个 AsyncTask 概念相当困惑,你介意看看我更新的代码吗?如果是这样,我已经更新了原始帖子以包含我的进度。我的主要斗争是参数。再次感谢。
    • 我对您的代码做了一些更正:pastebin.com/kVNZqSz3 1) 您可以将 URI 的字符串传递给 AsyncTask 2) 您可以从 doInBackground 返回字符串结果并在 onPostExecute 中使用它 3) 您不能在 doInBackgound 中使用 setText
    • 天哪!有效!哈哈终于成功了!非常感谢。
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2015-08-28
    • 2016-12-27
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多