So I'm fairly new to Java and programming with android in particular. I'm trying to create an app that pulls data from a financial website (perhaps one with an API if it makes it easier).
The first step I tried was to just pull any text off a site. I'm currently practicing with a .txt URL and this is my code thus far:
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");
}
}
}
The code prints "hello2". I'm not quite sure whats wrong or how to fix the issue so the try block works.
I also added this into my manifest:
<uses-permission
android:name="android.permission.INTERNET" />
I didn't get prompted for the app to allow internet access, is it automatic?
Thanks for any help and guidance.
************Edit update: I added comments to indicate the areas of confusion
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);
}
}
}