1. AndroidManifest.xml加入权限:
/>
2. android3.0以上凡是涉及到网络,下载等耗时操作,都不能在主线程中运行,不允许直接在ui线程直接操作httpClient
所以方法一:另起线程去访问。
方法二:如果不想另起线程,加上以下代码,可以取消严格限制
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
3.获取网页内容代码:
代码一:
packageList.com.list;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
importorg.json.JSONArray;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.ListView;
publicclassListActivityextendsActivity {
/** Called when the activity is first created. */
publicButton b =null;
publicString s=null;
publicListView listview1=null;
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button1);
listview1=(ListView) findViewById(R.id.listview1);
b.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
HttpGet httpget = newHttpGet("http://192.168.0.110:80/json/index.php");
HttpResponse httpresponse;
try{
//执行gethttp提交
httpresponse = newDefaultHttpClient().execute(httpget);
if(httpresponse.getStatusLine().getStatusCode()==200){
//如果成功吧返回的数据转换成string类型
String s=EntityUtils.toString(httpresponse.getEntity());
Log.i("JSON",s);
//声明一个json数组
JSONArray jsonarray=newJSONArray(s);
//声明一个数据组,长度他json数组的长度一样
String[] data=newString[jsonarray.length()];
//循环输出
for(inti=0;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 arrayadapter=newArrayAdapter(ListActivity.this, android.R.layout.simple_expandable_list_item_1,data);
//设置listview数据;
listview1.setAdapter(arrayadapter);
}
} catch(Exception e) {
Log.i("E",e.getMessage().toString());
}
}
});
}
}
代码二:
newThread(){
publicvoidrun(){
client = newDefaultHttpClient();
StringBuilder builder = newStringBuilder();
HttpGet myget = newHttpGet("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 = newBufferedReader(newInputStreamReader(entity.getContent()));
for(String s = reader.readLine(); s !=null; s = reader.readLine()) {
builder.append(s);
}
JSONObject jsonObject = newJSONObject(builder.toString());
String re_password = jsonObject.getString("password");
} catch(Exception e) {
e.printStackTrace();
}
}
}.start();
代码三
voidgetInput(){
try
{
URL url = newURL("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");
intresCode = conn.getResponseCode();
conn.connect();
InputStream stream = conn.getInputStream();
byte[] data=newbyte[102400];
intlength=stream.read(data);
String str=newString(data,0,length);
conn.disconnect();
System.out.println(str);
stream.close();
}
catch(Exception ee)
{
System.out.print("ee:"+ee.getMessage());
}
}
1. AndroidManifest.xml加入权限:
/>
2. android3.0以上凡是涉及到网络,下载等耗时操作,都不能在主线程中运行,不允许直接在ui线程直接操作httpClient
所以方法一:另起线程去访问。
方法二:如果不想另起线程,加上以下代码,可以取消严格限制
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
3.获取网页内容代码:
代码一:
packageList.com.list;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
importorg.json.JSONArray;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.ListView;
publicclassListActivityextendsActivity {
/** Called when the activity is first created. */
publicButton b =null;
publicString s=null;
publicListView listview1=null;
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button1);
listview1=(ListView) findViewById(R.id.listview1);
b.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
HttpGet httpget = newHttpGet("http://192.168.0.110:80/json/index.php");
HttpResponse httpresponse;
try{
//执行gethttp提交
httpresponse = newDefaultHttpClient().execute(httpget);
if(httpresponse.getStatusLine().getStatusCode()==200){
//如果成功吧返回的数据转换成string类型
String s=EntityUtils.toString(httpresponse.getEntity());
Log.i("JSON",s);
//声明一个json数组
JSONArray jsonarray=newJSONArray(s);
//声明一个数据组,长度他json数组的长度一样
String[] data=newString[jsonarray.length()];
//循环输出
for(inti=0;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 arrayadapter=newArrayAdapter(ListActivity.this, android.R.layout.simple_expandable_list_item_1,data);
//设置listview数据;
listview1.setAdapter(arrayadapter);
}
} catch(Exception e) {
Log.i("E",e.getMessage().toString());
}
}
});
}
}
代码二:
newThread(){
publicvoidrun(){
client = newDefaultHttpClient();
StringBuilder builder = newStringBuilder();
HttpGet myget = newHttpGet("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 = newBufferedReader(newInputStreamReader(entity.getContent()));
for(String s = reader.readLine(); s !=null; s = reader.readLine()) {
builder.append(s);
}
JSONObject jsonObject = newJSONObject(builder.toString());
String re_password = jsonObject.getString("password");
} catch(Exception e) {
e.printStackTrace();
}
}
}.start();
代码三
voidgetInput(){
try
{
URL url = newURL("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");
intresCode = conn.getResponseCode();
conn.connect();
InputStream stream = conn.getInputStream();
byte[] data=newbyte[102400];
intlength=stream.read(data);
String str=newString(data,0,length);
conn.disconnect();
System.out.println(str);
stream.close();
}
catch(Exception ee)
{
System.out.print("ee:"+ee.getMessage());
}
}