如何在android中阅读和使用网站内容
我需要一些帮助; 实际上,我必须在Android应用程序中阅读和使用某些网站的内容。我学习了一些教程,但没有成功。有人可以帮我 更新: 我实际上使用了两种不同的代码来获取网站的内容,但它们对我不起作用如何在android中阅读和使用网站内容,android,android-internet,Android,Android Internet,我需要一些帮助; 实际上,我必须在Android应用程序中阅读和使用某些网站的内容。我学习了一些教程,但没有成功。有人可以帮我 更新: 我实际上使用了两种不同的代码来获取网站的内容,但它们对我不起作用 public static String connect(String url) { String result = "bubububu" ; HttpClient httpclient = new DefaultHttpClient(); // Prepare a r
public static String connect(String url)
{
String result = "bubububu" ;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i("Praeda",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
result= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
return result ;
}
} catch (Exception e) {
e.getMessage() ;
}
return result ;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static String connect(String url)
{
String result = "bubububu" ;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i("Praeda",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
result= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
return result ;
}
} catch (Exception e) {
e.getMessage() ;
}
return result ;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
及
这两个都给了我一个例外。第一个在
response=httpclient.execute(httpget)和exception.getMessage()为“null”,而第二个在httpConn.setAllowUserInteraction(false)和exception.getMessage()处出现异常。连接时出错。即使我在menifest文件中使用了Internet权限看看这个问题的答案: 它有一个可以读取URL的代码
但是,最好更具体地说明StackOverflow,并解释您的问题到底是什么。对这个问题不是100%确定,但您可以使用Apache HTTPClient(建议用于预姜饼)或HTTPURLConnection(姜饼和更高版本)并执行GET以获取网页。从这里,您可以浏览原始数据(通常是HTML,返回文本)。现在有很多关于HTTPClient和HTTPURLConnection的好教程,所以我不在这里解释
另一个选择通常是WebView,我承认它可能很混乱。WebView允许您登录并执行诸如从下一页提取结果URL之类的操作。问题是,在Android设备上,WebView的行为是不一样的。你可能需要更具体地回答这个问题,但我会给出一个答案,如果你不允许从网站管理员那里获得Web服务,那不是一个好主意!谢谢朋友;你们在这里的意思更具体一些。我只有这些详细信息,即网站url,从这里获取一些特定内容,并在我的网站中使用这些内容app@Waza_Be... 我有网站管理员权限httpresponse response=httpclient.execute(httpget)给出异常,exception.getMessage()给出消息“null”,即使我添加了
private String DownloadText(String URL)
{
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString = String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return str;
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}