简体   繁体   English

从HTML响应中提取数据-Android

[英]Extracting data from a HTML response - Android

In my Android app, I am accessing a website using GET requests to its API. 在我的Android应用中,我正在使用对其API的GET请求访问网站。 The request returns an HTML file with a variety of user information. 该请求将返回带有各种用户信息的HTML文件。 From this, I want to know how to extract user data, such as their profile pictures, their data and such. 由此,我想知道如何提取用户数据,例如用户的个人资料图片,数据等。 I would like to know how I could access and retrieve this data to display in my app. 我想知道如何访问和检索这些数据以显示在我的应用程序中。

Any help will be greatly appreciated. 任何帮助将不胜感激。

Thanks in advance. 提前致谢。

If you just want to be able to access the data you could get the api to output in in JSON format and read the JSON from your app, something like: 如果您只想访问数据,则可以使该API以JSON格式输出并从您的应用读取JSON,例如:

public void getData(){

          String result = "";



          //http post

          try{
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://website.com/download.html");
                 HttpResponse response = httpclient.execute(httppost);
                 HttpEntity entity = response.getEntity();
                 InputStream is = entity.getContent();

                  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                  StringBuilder sb = new StringBuilder();
                  String line = null;
                  while ((line = reader.readLine()) != null) {
                          sb.append(line + "\n");
                  }
                  is.close();
                  result=sb.toString();

          }catch(Exception e){
                  Log.e("log_tag", "Error converting result "+e.toString());
          }



          //parse json data

          try{
                JSONArray jArray = new JSONArray(result);
                  for(int i=0;i<jArray.length();i++){
                          JSONObject json_data = jArray.getJSONObject(i);
                          names.add("   " + json_data.getString("name"));
                          age.add("   " + Integer.toString(json_data.getInt("age")));

                  }

          }catch(JSONException e){
                  Log.e("log_tag", "Error parsing data "+e.toString());

          }
}

The easiest way to go for HTML Parsing to extract and manipulate the data then go for JSoup ...... 最简单的方法是进行HTML语法分析以提取和操作数据,然后进行JSoup ......

See this link for more details: 有关更多详细信息,请参见此链接:

http://jsoup.org/ http://jsoup.org/

最简单的方法是在WebView中显示HTML数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM