android 获取php网页内容
来源:互联网 发布:大阪日式酒店推荐 知乎 编辑:程序博客网 时间:2024/04/28 22:00
http://blog.csdn.net/kaiqiangzhang001/article/details/8350938
http://www.kuqin.com/shuoit/20140108/337497.html
http://blog.csdn.net/lzz360/article/details/16887237
http://blog.sina.com.cn/s/blog_5a48dd2d0100tw0u.html
http://bbs.51cto.com/thread-954839-1.html
1. AndroidManifest.xml加入权限:
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2. android3.0以上凡是涉及到网络,下载等耗时操作,都不能在主线程中运行,不允许直接在ui线程直接操作httpClient
所以方法一:另起线程去访问。
方法二:如果不想另起线程,加上以下代码,可以取消严格限制
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
3.获取网页内容代码:
代码一:
package List.com.list; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.json.JSONArray;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView; public class ListActivity extends Activity {/** Called when the activity is first created. */ public Button b = null;public String s=null; public ListView listview1=null; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); b = (Button) findViewById(R.id.button1); listview1=(ListView) findViewById(R.id.listview1); b.setOnClickListener(new OnClickListener() { public void onClick(View v) {// TODO Auto-generated method stub HttpGet httpget = new HttpGet("http://192.168.0.110:80/json/index.php");HttpResponse httpresponse;try { //执行gethttp提交httpresponse = new DefaultHttpClient().execute(httpget); if(httpresponse.getStatusLine().getStatusCode()==200){//如果成功吧返回的数据转换成string类型String s=EntityUtils.toString(httpresponse.getEntity()); Log.i("JSON",s); //声明一个json数组JSONArray jsonarray=new JSONArray(s); //声明一个数据组,长度他json数组的长度一样String[] data=new String[jsonarray.length()]; //循环输出for(int i=0;i<jsonarray.length();i++){ Log.i("dd",jsonarray.getJSONObject(i).getString("ddd"));Log.i("tt",jsonarray.getJSONObject(i).getString("title")); //把结果存到一个数组里;data[i]=jsonarray.getJSONObject(i).getString("ddd")+jsonarray.getJSONObject(i).getString("title");} ArrayAdapter<String> arrayadapter=new ArrayAdapter<String>(ListActivity.this, android.R.layout.simple_expandable_list_item_1,data);//设置listview数据;listview1.setAdapter(arrayadapter); } } catch (Exception e) {Log.i("E",e.getMessage().toString()); } }}); }}
代码二:
new Thread(){public void run(){client = new DefaultHttpClient(); StringBuilder builder = new StringBuilder(); HttpGet myget = new HttpGet("http://10.0.2.2/testAndroid.php"); //HttpGet myget = new HttpGet("http://www.crazyit.org"); try { HttpResponse response = client.execute(myget); HttpEntity entity = response.getEntity();BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); for (String s = reader.readLine(); s != null; s = reader.readLine()) { builder.append(s); } JSONObject jsonObject = new JSONObject(builder.toString()); String re_password = jsonObject.getString("password"); } catch (Exception e) { e.printStackTrace(); } }}.start();
代码三
void getInput(){ try { URL url = new URL("http://www.google.cn/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setConnectTimeout(10000); conn.setRequestMethod("GET"); conn.setRequestProperty("accept", "*/*"); String location = conn.getRequestProperty("location"); int resCode = conn.getResponseCode(); conn.connect(); InputStream stream = conn.getInputStream(); byte[] data=new byte[102400]; int length=stream.read(data); String str=new String(data,0,length); conn.disconnect(); System.out.println(str); stream.close(); } catch(Exception ee) { System.out.print("ee:"+ee.getMessage()); } }
0 0
- android 获取php网页内容
- php获取网页内容
- php获取网页内容
- php获取网页内容
- 【php】获取网页内容
- PHP获取网页内容
- php 获取网页内容
- android 获取网页内容
- 使用php获取网页内容
- 使用php获取网页内容
- php获取网页内容方法
- PHP远程获取网页内容
- [PHP代码] PHP远程获取网页内容
- PHP 通过curl库函数获取网页内容
- PHP获取网页内容方法总结
- php 获取网页内容及代码
- php获取网页内容方法总结
- php获取网页内容方法总结发布
- Jquery处理table中每行中某个td的click事件
- android中跨进程通讯的4种方式
- Android 无缝轮播实现——本地加载图片轮播(一)
- "2>&1" 是什么意思
- 求二叉树中两结点的最小公共祖先
- android 获取php网页内容
- android vector
- window下symfony2安装以及写helloworld
- openstack 之 ceilometer: Alarm
- WinGDB远程调试FreeBSD程序无响应的解决办法
- jdk7 遍历文件树
- Linux IPC之共享内存
- 28个不得不看的经典编程算法
- java系统学习(十一) --------输入与输出(I/O)