首頁 > 後端開發 > php教程 > android 取得php網頁內容

android 取得php網頁內容

WBOY
發布: 2016-07-29 09:14:54
原創
918 人瀏覽過

http://blog.csdn.net/kaiqiangzhang001/article/details/8350938

http://www.kuqin.com/shuoit/20140108/337497.html /lzz360/article/details/16887237

http://blog.sina.com.cn/s/blog_5a48dd2d0100tw0u.html

http://bbs.51cto.com
1.

AndroidManifest.xml


加入權限:

permission android:name="android.permission

.INTERNET" />>> "android.permission.ACCESS_NETWORK_STATE" />2. 
android3.0以上凡是涉及到網絡,下載等耗時操作,都不能在主線程中運行,

不允許直接在ui不允許直接在ui httpClient

所以方法一:另起執行緒去存取。

方法二:如果不想另起線程,加上以下程式碼,可以取消嚴格限制StrictMode.ThreadPolicy policy=new StrictMode.ThreadStrictMode.ThreadPolicy policy=new StrictMode.Thready. ;  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.<strong>Activity</strong>;
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 List<strong>Activity</strong> extends <strong>Activity</strong> {
/** Called when the <strong>Activity</strong> is first created. */
 
public Button b = null;
public String s=null;
 
public ListView listview1=null;
 
@Override
public 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 js JSONArray(s);
 
//声明一个数据组,长度他json数组的长度一样
String[] data=new String[jsonarray.length()];
 
//<strong>循环</strong>输出
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>(List<strong>Activity</strong>.this, android.R.layout.simple_expandable_list_item_1,data);
//设置listview数据;
listview1.setAdapter(arrayadapter);
 
}
 
} catch (<strong>Exception</strong> 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 (<strong>Exception</strong> 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(<strong>Exception</strong> ee)  
{  
System.out.print("ee:"+ee.getMessage());   
}  
} 
登入後複製

以上就介紹了android 取得php網頁內容,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板