我的目标是创建一个非常基本的应用程序来读取HTML并将其存储到字符串中。我只对该网站的一行感兴趣。我找到了一个建议的主题:
String bodyHtml = "null";
try {
String myUri = "http://www.spring8.or.jp/ext/ja/status/text.html";
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(myUri);
HttpResponse response = httpClient.execute(get);
// Build up result
bodyHtml = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
}
url.setText(bodyHtml);
将url作为我的textview。据我所知,我已正确设置清单中的权限。
然而,当我在手机和模拟器上运行此代码时,它似乎根本不起作用。我一无所获。我错过了什么吗?
谢谢
答案 0 :(得分:0)
尝试使用此而不是EntityUtils
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
String newLine = "";
while ((line = rd.readLine()) != null) {
newLine = newLine.concat(line);
}
System.out.println(newLine);
答案 1 :(得分:0)
在HttpClient
的执行方法中,如下所示也放置HttpContext
:
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(get, localContext);
并使用BufferedReader
:
final BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
如果它不起作用,您可能会遇到Internet连接问题。 顺便说一句,不要忘记android.permission.INTERNET权限!
答案 2 :(得分:0)
试试这个,
调用以下方法下载HTml内容并在参数
中传递Urlprivate void downloadText(String urlStr) {
progressDialog = ProgressDialog.show(this, "",
"Download Text from " + urlStr);
final String url = urlStr;
new Thread () {
public void run() {
int BUFFER_SIZE = 2000;
InputStream in = null;
Message msg = Message.obtain();
msg.what=1;
try {
in = openHttpConnection(url);
InputStreamReader isr = new InputStreamReader(in);
int charRead;
text = "";
char[] inputBuffer = new char[BUFFER_SIZE];
while ((charRead = isr.read(inputBuffer))>0)
{
String readString =
String.copyValueOf(inputBuffer, 0, charRead);
text += readString;
inputBuffer = new char[BUFFER_SIZE];
}
Bundle b = new Bundle();
b.putString("text", text);
msg.setData(b);
in.close();
}catch (IOException e2) {
e2.printStackTrace();
}
messageHandler.sendMessage(msg);
}
}.start();
}
这是一个返回InputStream Object的辅助方法,
private InputStream openHttpConnection(String urlStr) {
InputStream in = null;
int resCode = -1;
try {
URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException ("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection)urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
现在使用Handler
在textView中显示Stringprivate Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
TextView text = (TextView) findViewById(R.id.textview01);
text.setText(msg.getData().getString("text"));
break;
}
progressDialog.dismiss();
}
};
在清单中提供 INTERNET 权限。