这个例子来自于Android学习手册,该手册包含9个章节和108个例子。所有的例子都是可交互和可运行的,并且源码采用了Android Studio目录结构,代码部分采用高亮显示。文档的结构图能够帮助快速定位所需内容。你可以通过360手机助手下载该学习手册,该应用的图标上有一个贝壳标志。
//第一种
/**获取参数(ArrayListnameValuePairs,String url)后post给远程服务器
* 将获得的返回结果(String)返回给调用者
* 本函数适用于查询数量较少的时候
*/
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;
}
需要在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);
}
展开全部
首先你需要导入:HttpClient、HttpGet
然后通过HttpGet通过url发送request
通过HttpClient的execute获取response
解析返回的内容
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();
以上是获取网页数据的方法,适用于Android系统的指南的详细内容。更多信息请关注PHP中文网其他相关文章!