android url网络请求+json解析
2014-12-08 18:39
615 查看
activity:
public class ProfessionActivity extends Activity{
private static String SIZE = "qweplj";
private ListView mlistView;
public String readJSONFeed(String URL){
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode=statusLine.getStatusCode();
if(statusCode==200){
HttpEntity entity = response.getEntity();
InputStream content= entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line ;
while((line = reader.readLine())!=null){
stringBuilder.append(line);
}
}else{
Log.e("JSON", "failed to download file");
}
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String,Void,String>{
ArrayList<ProfessionCategoryDetail> ProfessionCategoryDetailList =new ArrayList<ProfessionCategoryDetail>();
@Override
protected void onPostExecute(String result){
mlistView = (ListView) findViewById(R.id.activity02ListView);
try{
JSONArray jsonArray=new JSONArray(result);
Log.i(SIZE, result);
Log.i("JSON", "number of surveys in feed:" + jsonArray.length());
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
ProfessionCategoryDetail mMusicCategoryDetail = new ProfessionCategoryDetail();
mMusicCategoryDetail.setId(jsonObject.getString("profession_id"));
mMusicCategoryDetail.setName(jsonObject.getString("profession_name"));
mMusicCategoryDetail.setKind(jsonObject.getString("profession_kind"));
mMusicCategoryDetail.setDegree(jsonObject.getString("profession_degree"));
ProfessionCategoryDetailList.add(mMusicCategoryDetail);
}
Activity2Adapter mActivity2Adapter = new Activity2Adapter(ProfessionActivity.this,ProfessionCategoryDetailList);
ProfessionActivity.this.mlistView.setAdapter(mActivity2Adapter);
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
Toast.makeText(
ProfessionActivity.this,
"位置:" + position + "内容:"
+ ProfessionCategoryDetailList.get(position).getName(), Toast.LENGTH_LONG).show();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ReadJSONFeedTask().execute("http://192.168.1.253/index.php/Home/Jiekoutest/allProfessionInfo/kind/1");
}
public class Activity2Adapter extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<ProfessionCategoryDetail> mMusicListItems;
public Activity2Adapter(Activity paramActivity, ArrayList<ProfessionCategoryDetail> paramList)
{
this.mInflater = (LayoutInflater) paramActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
this.mMusicListItems = paramList;
}
public int getCount()
{
// TODO Auto-generated method stub
return mMusicListItems.size();
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return mMusicListItems.get(position);
}
public long getItemId(int paramInt)
{
return paramInt;
}
public View getView(int position, View convertView, ViewGroup parent)
{ // TODO Auto-generated method stub
ImageView localImageView = null;
TextView localTextView1 = null;
TextView localTextView2 = null;
TextView localTextView3 = null;
TextView localTextView4 = null;
ProfessionCategoryDetail localMusicCategoryDetail = (ProfessionCategoryDetail)getItem(position);
if (localMusicCategoryDetail != null)
{
if (null == convertView)
{
convertView = mInflater.inflate(R.layout.listitem, parent, false);
localImageView = (ImageView) convertView.findViewById(R.id.listitem02ImageView);
localTextView1 = (TextView) convertView.findViewById(R.id.listitem02TextView01);
localTextView2 = (TextView) convertView.findViewById(R.id.listitem02TextView02);
localTextView3 = (TextView) convertView.findViewById(R.id.listitem02TextView03);
localTextView4 = (TextView) convertView.findViewById(R.id.listitem02TextView04);
}
else
{
localImageView = (ImageView) convertView.findViewById(R.id.listitem02ImageView);
localTextView1 = (TextView) convertView.findViewById(R.id.listitem02TextView01);
localTextView2 = (TextView) convertView.findViewById(R.id.listitem02TextView02);
localTextView3 = (TextView) convertView.findViewById(R.id.listitem02TextView03);
localTextView4 = (TextView) convertView.findViewById(R.id.listitem02TextView04);
}
if(null!=convertView)
{
localImageView.setImageResource(R.drawable.i1);
localTextView1.setText("[Id]:"+localMusicCategoryDetail.getId());
localTextView2.setText("[Name]:"+localMusicCategoryDetail.getName());
localTextView3.setText("[Kind]:"+localMusicCategoryDetail.getKind());
localTextView4.setText("[Degree]:"+localMusicCategoryDetail.getDegree());
}
}
return convertView;
}
}
}
ProfessionCategoryDetail:
public class ProfessionCategoryDetail {
private String profession_name;
private String profession_id;
private String profession_degree;
private String profession_kind;
public ProfessionCategoryDetail(){
super();
}
public String getName(){
return profession_name;
}
public void setName(String profession_name){
this.profession_name = profession_name;
}
public String getId(){
return profession_id;
}
public void setId(String profession_id){
this.profession_id = profession_id;
}
public String getDegree(){
return profession_degree;
}
public void setDegree(String profession_degree){
this.profession_degree=profession_degree;
}
public String getKind(){
return profession_kind;
}
public void setKind(String profession_kind){
this.profession_kind=profession_kind;
}
public ProfessionCategoryDetail(String profession_name, String profession_id, String profession_degree, String profession_kind){
super();
this.profession_name = profession_name;
this.profession_id = profession_id;
this.profession_degree = profession_degree;
this.profession_kind = profession_kind;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id = "@+id/activity02ListView"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/listitem02ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/desc"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/listitem02TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/listitem02TextView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/listitem02TextView03"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/listitem02TextView04"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
public class ProfessionActivity extends Activity{
private static String SIZE = "qweplj";
private ListView mlistView;
public String readJSONFeed(String URL){
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode=statusLine.getStatusCode();
if(statusCode==200){
HttpEntity entity = response.getEntity();
InputStream content= entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line ;
while((line = reader.readLine())!=null){
stringBuilder.append(line);
}
}else{
Log.e("JSON", "failed to download file");
}
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String,Void,String>{
ArrayList<ProfessionCategoryDetail> ProfessionCategoryDetailList =new ArrayList<ProfessionCategoryDetail>();
@Override
protected void onPostExecute(String result){
mlistView = (ListView) findViewById(R.id.activity02ListView);
try{
JSONArray jsonArray=new JSONArray(result);
Log.i(SIZE, result);
Log.i("JSON", "number of surveys in feed:" + jsonArray.length());
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
ProfessionCategoryDetail mMusicCategoryDetail = new ProfessionCategoryDetail();
mMusicCategoryDetail.setId(jsonObject.getString("profession_id"));
mMusicCategoryDetail.setName(jsonObject.getString("profession_name"));
mMusicCategoryDetail.setKind(jsonObject.getString("profession_kind"));
mMusicCategoryDetail.setDegree(jsonObject.getString("profession_degree"));
ProfessionCategoryDetailList.add(mMusicCategoryDetail);
}
Activity2Adapter mActivity2Adapter = new Activity2Adapter(ProfessionActivity.this,ProfessionCategoryDetailList);
ProfessionActivity.this.mlistView.setAdapter(mActivity2Adapter);
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
Toast.makeText(
ProfessionActivity.this,
"位置:" + position + "内容:"
+ ProfessionCategoryDetailList.get(position).getName(), Toast.LENGTH_LONG).show();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ReadJSONFeedTask().execute("http://192.168.1.253/index.php/Home/Jiekoutest/allProfessionInfo/kind/1");
}
public class Activity2Adapter extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<ProfessionCategoryDetail> mMusicListItems;
public Activity2Adapter(Activity paramActivity, ArrayList<ProfessionCategoryDetail> paramList)
{
this.mInflater = (LayoutInflater) paramActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
this.mMusicListItems = paramList;
}
public int getCount()
{
// TODO Auto-generated method stub
return mMusicListItems.size();
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return mMusicListItems.get(position);
}
public long getItemId(int paramInt)
{
return paramInt;
}
public View getView(int position, View convertView, ViewGroup parent)
{ // TODO Auto-generated method stub
ImageView localImageView = null;
TextView localTextView1 = null;
TextView localTextView2 = null;
TextView localTextView3 = null;
TextView localTextView4 = null;
ProfessionCategoryDetail localMusicCategoryDetail = (ProfessionCategoryDetail)getItem(position);
if (localMusicCategoryDetail != null)
{
if (null == convertView)
{
convertView = mInflater.inflate(R.layout.listitem, parent, false);
localImageView = (ImageView) convertView.findViewById(R.id.listitem02ImageView);
localTextView1 = (TextView) convertView.findViewById(R.id.listitem02TextView01);
localTextView2 = (TextView) convertView.findViewById(R.id.listitem02TextView02);
localTextView3 = (TextView) convertView.findViewById(R.id.listitem02TextView03);
localTextView4 = (TextView) convertView.findViewById(R.id.listitem02TextView04);
}
else
{
localImageView = (ImageView) convertView.findViewById(R.id.listitem02ImageView);
localTextView1 = (TextView) convertView.findViewById(R.id.listitem02TextView01);
localTextView2 = (TextView) convertView.findViewById(R.id.listitem02TextView02);
localTextView3 = (TextView) convertView.findViewById(R.id.listitem02TextView03);
localTextView4 = (TextView) convertView.findViewById(R.id.listitem02TextView04);
}
if(null!=convertView)
{
localImageView.setImageResource(R.drawable.i1);
localTextView1.setText("[Id]:"+localMusicCategoryDetail.getId());
localTextView2.setText("[Name]:"+localMusicCategoryDetail.getName());
localTextView3.setText("[Kind]:"+localMusicCategoryDetail.getKind());
localTextView4.setText("[Degree]:"+localMusicCategoryDetail.getDegree());
}
}
return convertView;
}
}
}
ProfessionCategoryDetail:
public class ProfessionCategoryDetail {
private String profession_name;
private String profession_id;
private String profession_degree;
private String profession_kind;
public ProfessionCategoryDetail(){
super();
}
public String getName(){
return profession_name;
}
public void setName(String profession_name){
this.profession_name = profession_name;
}
public String getId(){
return profession_id;
}
public void setId(String profession_id){
this.profession_id = profession_id;
}
public String getDegree(){
return profession_degree;
}
public void setDegree(String profession_degree){
this.profession_degree=profession_degree;
}
public String getKind(){
return profession_kind;
}
public void setKind(String profession_kind){
this.profession_kind=profession_kind;
}
public ProfessionCategoryDetail(String profession_name, String profession_id, String profession_degree, String profession_kind){
super();
this.profession_name = profession_name;
this.profession_id = profession_id;
this.profession_degree = profession_degree;
this.profession_kind = profession_kind;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id = "@+id/activity02ListView"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/listitem02ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/desc"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/listitem02TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/listitem02TextView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/listitem02TextView03"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/listitem02TextView04"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
相关文章推荐
- Android使用HttpURLConnection请求网络返回JSON数据并解析
- Android下网络请求数据,返回数据显示不全,解析json no value for xxx
- Android中异步解析网络请求的URL
- Android中解析网络请求的URL
- Android okHttp网络请求之Json解析(三)
- [置顶] Android网络与数据存储03-在线请求天气API,并解析其中的json数据予以显示
- Android okHttp 实战(三):okHttp网络请求之Json解析
- Android数据解析之JSON,网络编程之URL,异步任务AsyncTask讲解
- Android--使用原生技术实现ListView(原生技术实现网络异步请求,解析json数据)
- Android okHttp网络请求之Json解析
- Android商城开发系列(六)——使用 OkHttpUtils 请求网络 + 使用 fastjson解析数据
- Android中解析网络请求的URL
- Android--网络请求与json数据解析的使用
- Android请求网络数据,json解析-FastJson遇到的问题 首字母大写问题
- android 网络请求+json解析 最优分析
- Android网络请求与JSON解析和数据抓取小实例
- android解析JSON,XML数据的请求网络工具类
- android:通过url向网络端获取json格式数据并解析显示
- Android网络与数据存储——网络编程数据处理(网络请求解析Json,解析xml)
- C#中解析json以及网络请求