您的位置:首页 > 理论基础 > 计算机网络

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>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: