这可能真的很简单,但我对制作应用程序很陌生,事实上这是我的第一次尝试。
我想知道我应该如何为我的应用程序编写代码以访问我的网站、加载文本文件并将其存储为变量。
我有一个文件“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);
}
}