0

这可能真的很简单,但我对制作应用程序很陌生,事实上这是我的第一次尝试。

我想知道我应该如何为我的应用程序编写代码以访问我的网站、加载文本文件并将其存储为变量。

我有一个文件“text.txt”并将其上传到这里“ http://my-test.com/app/ ”但我不知道如何让我的应用程序从文本文件中加载数据作为变量我可以使用。所以我做了一些谷歌搜索并在这里查看。

这篇文章:如何从网络读取 android 中的文本文件?非常相关,我试图遵循它,这就是我已经走了多远

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");
}

我不知道为什么,但我正在使用的程序(eclipse)仍然给我一个灯泡和红十字故障,它亲切地说“httpclient无法解决”这需要解决吗?我该如何解决这个问题?它会起作用吗?

所有代码

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);
}
}
4

1 回答 1

0

httpclient在您的代码HttpResponse response= httpclient.execute(httppost);中尚未定义,因此您需要定义它。我认为查看您将要使用 ApacheHttpClient 的代码,请在此处查看此 Vogella 教程以获取更多信息(您想要的行是HttpClient httpclient = new DefaultHttpClient()

编辑:

对于你的第二个问题,所有这些代码都在这里

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");
}

应该在方法调用中。目前 this 在类初始化时被调用,这意味着你将在httpclient.execute(httppost);被调用时阻塞线程。如果你把它放在一个新的方法中,然后调用那个方法,onResume()你会有更多的运气,也许是这样的:

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();
}
于 2013-10-10T02:04:38.640 回答