0

This is probably really simple but I am very new to making apps, in fact this is my first attempt.

I want to know how I should go about writing the code for my app to go to my website, load a text file and store it as a variable.

I have a file "text.txt" and I upload it to here "http://my-test.com/app/" But I don't know how to make my app load the data from the text file as a variable that I can use. So I did some Google searches and looked on here.

This post: How to read text file in android from web? Is very relevant and I've tried to follow it and this is how far I've got

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;

HttpGet httppost = new HttpGet("http://my-test.com/app/text.txt");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();

BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
    total.append(line + "\n");
}

I don't know why but the program I'm using (eclipse) is giving me a light bulb and red cross of failure still and it kindly says "httpclient cannot be resolved" Does this need to be resolved? how do I resolve this? and will it work?

All the code

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {

HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet("http://my-test.com/app/text.txt");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();

BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
static String line = ""
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}

static private final String DEVELOPER_KEY = "";
private static final String VIDEO = line;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);
    youTubeView.initialize(DEVELOPER_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
    Toast.makeText(this, "Oh no! "+error.toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
player.loadVideo(VIDEO);
}
}
Community
  • 1
  • 1

1 Answers1

0

httpclient in your code HttpResponse response= httpclient.execute(httppost); hasn't been defined yet, so you will need to define this. I think looking at the code you will want to use the ApacheHttpClient, have a look at this Vogella tutorial here for more info (the line you want is HttpClient httpclient = new DefaultHttpClient()

EDIT:

for your second question, all this code here

HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet("http://my-test.com/app/text.txt");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();

BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line = ""
while ((line = r.readLine()) != null) {
    total.append(line + "\n");
}

should be inside a method call. At the moment this is called when the class is initialized, which means you will block the thread when httpclient.execute(httppost); is called. if you put it in side a new method, and then call that method from onResume() then you will have more luck, perhaps like this:

private String requestData(String url){
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httppost = new HttpGet("http://my-test.com/app/text.txt");

    // this should be done in a background thread (or AsyncTask), but we'll leave it for now
    HttpResponse response = httpclient.execute(httppost); 

    HttpEntity ht = response.getEntity();
    BufferedHttpEntity buf = new BufferedHttpEntity(ht);
    InputStream is = buf.getContent();

    BufferedReader r = new BufferedReader(new InputStreamReader(is));
    StringBuilder total = new StringBuilder();
    String line = ""
    while ((line = r.readLine()) != null) {
        total.append(line + "\n");
    }

    // return the data we received to our caller
    return total.toString();
}
panini
  • 2,026
  • 19
  • 21
  • Yes that was very helpful, thank you. There is now one more thing that I don't know. That is, how do I call the data from the file? I think the while loop isn't needed for this case as there is only one line of 16 characters in the file, nevertheless how do I get that data here... static private final String MYTEXT = ???; That is where the question marks are. –  Oct 10 '13 at 03:45
  • @user2865082 I'm not sure what the data in your file looks like, but you should now have it in String form so you should be able to manipulate it from there. Otherwise, if you post the structure of your file, I can try and help you with that. Please accept the answer if it helped your problem :) – panini Oct 10 '13 at 03:45
  • the data in the .txt file looks like this **"ehcMexkVomYaFqoI"** _Cannot make a static reference to the non-static field total_ is my problem so should I change the `String line;` to `static String line = "";`? –  Oct 10 '13 at 19:26
  • the field "total" is not static, but you're trying to reference it statically, that is your problem. I don't think there is a problem with String line;. maybe post all your code in the OP so we can see what you are trying to do? – panini Oct 10 '13 at 20:43