android端和服务器端通信,交互信息,参数采用JSON,使用了HttpClient与HttpPost类

Android端的程序和配置

1:androidmanifest.xml的内容为:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="cn.capinfotech.json"  
      android:versionCode="1"  
      android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".MainActivity"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
  
    </application>  
    <uses-sdk android:minSdkVersion="8" />  
    <uses-permission android:name="android.permission.INTERNET" />  
  
</manifest>   

2:main.xml的内容主要是定义了ListView用来显示最新的电影咨询

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
  
<ListView   
    android:id="@+id/videos"  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
   />  
</LinearLayout>  

3:item.xml的内容,主要用来定义ListView里每个元素的显示方式

<?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">  
<TextView  
  android:layout_width="250dip"  
  android:layout_height="wrap_content"  
  android:id="@+id/title"   
/>  
  
<TextView  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:id="@+id/timelength"  
/>  
  
</LinearLayout>  

4:定义了一个实体类Video

package com.capinfotech.model;  
  
public class Video {  
      
    private Integer id;  
    private String name;  
    private Integer time;  
      
    public Video() {  
          
    }  
  
    public Video(Integer id, String name, Integer time) {  
        super();  
        this.id = id;  
        this.name = name;  
        this.time = time;  
    }  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public Integer getTime() {  
        return time;  
    }  
  
    public void setTime(Integer time) {  
        this.time = time;  
    }  
  
}  

5:MainActivity的内容

package cn.capinfotech.json;  
  
import java.net.URI;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.client.HttpClient;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.impl.client.DefaultHttpClient;  
import org.apache.http.util.EntityUtils;  
import org.json.JSONArray;  
import org.json.JSONObject;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.util.Log;  
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
import android.widget.Toast;  
  
public class MainActivity extends Activity {  
     private static final String TAG = "MainActivity";  
     private List<HashMap<String, Object>> videos = null;  
     private HashMap<String, Object> video = null;  
       
     private ListView listView = null;  
     private static String url = "http://10.0.2.2:8088/getjson.php";    
       
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        listView = (ListView)findViewById(R.id.videos);  
        getPDAServerData(url);  
          
    }  
   
    private void getPDAServerData(String url) {    
            HttpClient client = new DefaultHttpClient();    
            //提拱默认的HttpClient实现    
            HttpPost request;    
            try {    
                request = new HttpPost(new URI(url));    
                HttpResponse response = client.execute(request);    
                // 判断请求是否成功    
                if (response.getStatusLine().getStatusCode() == 200) { //200表示请求成功    
                    HttpEntity entity = response.getEntity();    
                    if (entity != null) {    
                        String out = EntityUtils.toString(entity, "UTF-8");    
                        Log.i(TAG, out);  
                          
                        JSONArray jsonArray = new JSONArray(out);  
                          
                        videos = new ArrayList<HashMap<String, Object>>();  
                        for(int i = 0; i<jsonArray.length(); i++) {  
                            JSONObject jsonObject = (JSONObject) jsonArray.get(i);  
                            int id = jsonObject.getInt("id");  
                            String name = jsonObject.getString("title");  
                            int timelength = jsonObject.getInt("timelength");  
                              
                            video = new HashMap<String, Object>();  
                            video.put("id", id);  
                            video.put("name", name);  
                            video.put("timelength", "时长为:" + timelength);  
                              
                            videos.add(video);  
                        }  
                          
                        SimpleAdapter adapter = new SimpleAdapter(this, videos, R.layout.item,   
                                                               new String[]{"name", "timelength"},  
                                                               new int[]{R.id.title, R.id.timelength}  
                                                 );  
                        listView.setAdapter(adapter);  
                          
                    }  
                }  
            } catch(Exception e) {  
                e.printStackTrace();  
                Log.e(TAG, e.toString());  
                Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_LONG).show();  
            }  
    }  
}  

6:程序界面效果图

留下评论

鄂ICP备13000209号-1

鄂公网安备 42050602000277号