大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先有个工具类: Myhttp.class
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、做网站、高平网络推广、重庆小程序开发、高平网络营销、高平企业策划、高平品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供高平建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
package com.example.json; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class Myhttp { public String httpGet(String url) { String response = null; HttpClient httpClient = new DefaultHttpClient(); //创建HttpGet对象 HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse; try { //使用execute方法发送 HttpGet请求,并返回httRresponse对象 httpResponse = httpClient.execute(httpGet); int statusCode = httpResponse.getStatusLine().getStatusCode(); if(statusCode==HttpStatus.SC_OK) { //获得返回结果 response=EntityUtils.toString(httpResponse.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } }
然后主类
package com.example.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { ListView list; ArrayList> myArrayList = new ArrayList >(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = (ListView) findViewById(R.id.list); new getJsonTask().execute(null, null, null); } class getJsonTask extends AsyncTask