使用Android Java从web上提取数据
所以我对Java相当陌生,尤其是对android编程。我正在尝试创建一个应用程序,从一个金融网站(可能是一个带有API的应用程序,如果它更容易的话)提取数据 我尝试的第一步就是从网站上提取任何文本。我目前正在使用.txt URL进行练习,这是我迄今为止的代码:使用Android Java从web上提取数据,java,android,web-scraping,data-mining,Java,Android,Web Scraping,Data Mining,所以我对Java相当陌生,尤其是对android编程。我正在尝试创建一个应用程序,从一个金融网站(可能是一个带有API的应用程序,如果它更容易的话)提取数据 我尝试的第一步就是从网站上提取任何文本。我目前正在使用.txt URL进行练习,这是我迄今为止的代码: package com.example.datatesting; import java.io.IOException; import java.io.BufferedInputStream; import java.io.InputS
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" />
我没有收到允许上网的应用程序提示,它是自动的吗
谢谢你的帮助和指导
************编辑更新:我添加了注释,以指示混乱的区域
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);
}
}
}
公共类MainActivity扩展活动{
私家图文电视;
私有字符串myString=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
//我不确定要在execute(…)中放入什么,所以我在这里添加了这个,但这需要
//一个try-catch块,可以返回到我的原始版本。。。
URL myURL=新URL(“http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
新建DataExtract().execute(myURL);
}
私有类DataExtract扩展了异步任务{
受保护的Void doInBackground(URL…URL){//这需要一个返回类型,但我不返回任何内容
试一试{
URL myURL=新URL(“http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
URLConnection ucon=myURL.openConnection();
InputStream=ucon.getInputStream();
BufferedInputStream bis=新的BufferedInputStream(is);
ByteArrayBuffer baf=新ByteArrayBuffer(50);
int电流=0;
而((当前=bis.read())!=-1){
baf.append((字节)当前值);
}
myString=新字符串(baf.toByteArray());
tv.setText(“hello1”);
}
捕获(例外e){
myString=e.getMessage();
tv.setText(“hello2”);
}
}
受保护的void onPostExecute(void result){//这是一个可接受的参数吗?
tv.setText(myString);
}
}
}
onCreate
在应用程序的主(或UI)线程中执行
如果您尝试在UI线程中执行网络操作,您将得到一个NetworkOnMainThreadException
解决这个问题的一种方法是在答案中使用like
还请注意,您不能从
doInBackground
中触摸视图。您可以从<代码> OnPistExc>/Calp>中将值设置为<代码>文本视图>代码>。< /P>我的Android的基本基础围绕使用OnCube和StTraceVIEW视图。我目前正在尝试使用AsyncTask。我创建了一个新的私有类,我很难找到引用TextView的方法。为了清楚起见,我应该将try-catch代码移动到扩展AsyncTask?的新类中。您的文本视图可以是MainActivity的私有成员。您的类(扩展AsyncTask)可以是MainActivity的私有类成员。谢谢你的帮助!我对整个AsyncTask概念相当困惑,您介意看看我更新的代码吗?如果是这样的话,我已经更新了原来的帖子,包括我的进度。我的主要斗争是参数。再次感谢。我对您的代码做了一些更正:1)您可以将URI的字符串传递给AsyncTask 2)您可以从doInBackground返回字符串结果并在onPostExecute中使用它3)您不能在doInBackgoundOh sweet中使用setText!!成功了!哈哈,终于成功了!非常感谢。