Home >Computer Tutorials >Computer Knowledge >How to obtain web page data, a guide for Android systems

How to obtain web page data, a guide for Android systems

王林
王林forward
2024-01-24 08:57:111084browse

How to obtain web page data, a guide for Android systems

How to let Android get the data on the webpage

This example comes from the Android Learning Manual, which contains 9 chapters and 108 examples. All examples are interactive and runnable, and the source code adopts the Android Studio directory structure, and the code part is highlighted. The structure diagram of the document can help you quickly locate what you need. You can download the study manual through 360 Mobile Assistant. The icon of the application has a shell logo.

//The first

/**Get the parameters (ArrayListnameValuePairs, String url) and post to the remote server

* Return the obtained return result (String) to the caller

* This function is suitable for when the number of queries is small

*/

public String posturl(ArrayListnameValuePairs,String url){

String result = "";

String tmp= """;

InputStream is = null;

try{

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

is = entity.getContent();

}catch(Exception e){

return "Fail to establish http connection!";

}

try{

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));

StringBuilder sb = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null) {

sb.append(line "\n");

}

is.close();

tmp=sb.toString();

}catch(Exception e){

return "Fail to convert net stream!";

}

try{

JSONArray jArray = new JSONArray(tmp);

for(int i=0;iJSONObject json_data = jArray.getJSONObject(i);

Iteratorkeys=json_data.keys();

while(keys.hasNext()){

result = json_data.getString(keys.next().toString());

}

}

}catch(JSONException e){

return "The URL you post is wrong!";

}

return result;

}

How to obtain Internet access records in android development

Need to add permissions in AndroidManifest.xml.

String string = null;

ContentResolver contentResolver = getContentResolver();

Cursor cursor = contentResolver.query(Uri.parse("content://browser/bookmarks"), new String[]{"url"}, null, null, null);

while (cursor != null & cursor.moveToNext()) {

string = cursor.getString(cursor.getColumnIndex("url"));

Log.d("debug", string == null ? "null":string);

}

How to get the data of html page when Android calls html page

Expand All

First you need to import: HttpClient, HttpGet

Then send the request through the url through HttpGet

Get response through HttpClient's execute

Parse the returned content

HttpClient client = new DefaultHttpClient();

HttpGet request = new HttpGet(url);

HttpResponse response = client.execute(request);

String html = """;

InputStream in = response.getEntity().getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

StringBuilder str = new StringBuilder();

String line = null;

while((line = reader.readLine()) != null)

{

str.append(line);

}

in.close();

html = str.toString();

The above is the detailed content of How to obtain web page data, a guide for Android systems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Fix dual boot menuNext article:Fix dual boot menu