android解析php輸出的json格式字符串實例

在服務器端使用php從mysql數據庫中查找數據出來,然後構造成json,等待客戶端以get方式獲取。
本例子基本在上次的例子上面進行。圖片效果如下:
vcq9u/HIobf+zvHG97bLUEhQIEpzb263tbvYyv2+3Q==” src=”/wp-content/images1/20181014/20120920041604819207.jpg” />
   
訪問頁面的json數據為:
{“value”:[{"nid":"1","title":"迎新","keyword":"迎新","date_time":"1309791386","content":"某某學院將於XX年XX月XX日XX點於XX地舉辦XX屆迎新晚會! "},{"nid":"2","title":"南嶺校區藝術活動中心","keyword":"南嶺,藝術","date_time":"1309791498","content":"賽後,康永剛處長致閉幕辭,祝賀比賽圓滿結束,恭喜參賽選手斬獲佳績。"}]}
[java] 
index.php代碼 
 
<?php 
    header("Content-Type: text/html; charset=UTF-8"); 
    $type = $_GET['type']; 
    if($type == 1) 
    { 
        //$obj->title = "Test"; 
        //$obj->id = 1; 
        //$obj->value = urlencode("TTT"); 
        //echo urldecode ( json_encode ($obj)); 
        $array = array( 'title'=>'TTT', 'id'=>1, 'value'=>urlencode("測試")); 
        echo urldecode(json_encode($array)); 
    } 
    else 
    { 
        $con = mysql_connect ( "localhost", "root", "windows" ); 
        if (!$con) 
        { 
            die ( 'Could not connect: ' . mysql_error () ); 
        } 
        mysql_select_db ( "dg", $con ); 
        mysql_query ( "set names utf8" ); 
        $query = "select * from p_news"; 
        $result = mysql_query ( $query ); 
        while ( $row = mysql_fetch_assoc ( $result ) ) 
        { 
            $response [] = $row; 
        } 
        foreach ( $response as $key => $value ) 
        { 
            $newData[$key] = $value; 
            $newData [$key] ['title'] = urlencode ( $value ['title'] ); 
            $newData [$key] ['keyword'] = urlencode ( $value ['keyword'] ); 
            $newData [$key] ['content'] = urlencode ( $value ['content'] ); 
        } 
        echo urldecode ( json_encode ( array("value"=>$newData) )); 
                  mysql_free_result($result); 
        mysql_close ( $con ); 
    } 
?> 
Android中Java代碼如下: 
 
public void getServerJsonDataWithType(String url,EditText editText) 
    { 
        int res = 0; 
        HttpClient client = new DefaultHttpClient(); 
        StringBuilder str = new StringBuilder(); 
        HttpGet httpGet = new HttpGet(url); 
        BufferedReader buffer = null; 
        try 
        { 
            HttpResponse httpRes = client.execute(httpGet); 
            httpRes = client.execute(httpGet); 
            res = httpRes.getStatusLine().getStatusCode(); 
            if(res == 200) 
            { 
                buffer = new BufferedReader(new InputStreamReader(httpRes.getEntity().getContent(),Charset.forName("UTF-8"))); 
                for(String s = buffer.readLine(); s != null ; s = buffer.readLine()) 
                { 
                    str.append(s); 
                } 
                Log.i(Tag,str.toString()); 
                buffer.close(); 
                JSONArray json = new JSONObject(str.toString()).getJSONArray("value"); 
                String strs = ""; 
                for(int i=0;i<json.length();i++) 
                { 
                    JSONObject jsonObject = (JSONObject)json.opt(i); 
                    strs += jsonObject.getString("content"); 
                } 
                //String title = json.getString("title"); 
                //int id = json.getInt("id"); 
                //String value = json.getString("value"); 
                editText.setText(strs); 
            } 
            else 
            { 
                Log.i(Tag, "HttpGet Error"); 
            } 
        } 
        catch(Exception e) 
        { 
            Log.i(Tag, "Exception"); 
            if(buffer != null) 
            { 
                try { 
                    buffer.close(); 
                } catch (IOException e1) { 
                    // TODO Auto-generated catch block 
                    e1.printStackTrace(); 
                } 
            } 
        } 
    } 
數據庫代碼如下:

CREATE

TABLE
IF NOT

EXISTS `p_news` (

  `nid`
int(11)
NOT

NULL
AUTO_INCREMENT,

  `title`
varchar(50)
NOT

NULL,

  `keyword`
varchar(25)
NOT

NULL,

  `date_time`
int(10)
NOT

NULL,

  `content`
 text NOT

NULL,

  PRIMARY

KEY
(`nid`)

)
 ENGINE=MyISAM  DEFAULT

CHARSET=utf8 AUTO_INCREMENT=5 ;

 


 Dumping data for table `p_news`

 

INSERT

INTO
`p_news` (`nid`, `title`, `keyword`, `date_time`, `content`) VALUES

(1,
'迎新',
'迎新',
 1309791386, '某某學院將於XX年XX月XX日XX點於XX地舉辦XX屆迎新晚會!
 '),

(2,
'南嶺校區藝術活動中心',
'南嶺,藝術',
 1309791498, '賽後,康永剛處長致閉幕辭,祝賀比賽圓滿結束

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。