所以我对Java很新,特别是使用android进行编程。我正在尝试创建一个从金融网站中提取数据的应用程序(如果它更容易,可能会使用API)。
我尝试的第一步是从网站上删除任何文字。我目前正在使用.txt网址练习,这是我迄今为止的代码:
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);
}
}
}