Android parses the json Format String output by php

Source: Internet
Author: User

Search for data from the mysql database using php on the server side and construct the data as json. Wait for the client to get the data in get mode.
This example is based on the previous example. The image effect is as follows:
Vcq9u/HIobf + zvHG97bLUEhQIEpzb263tbvYyv2 + 3Q = "src =" http://www.bkjia.com/uploads/allimg/131211/031ZM105-0.jpg "/>

The json data for accessing the page is:
{"Value": [{"nid": "1", "title": "Welcome", "keyword": "Welcome", "date_time": "1309791386 ", "content": "XX Emy will hold the XX at XX! "},{" Nid ":" 2 "," title ":" Nanling campus art activity center "," keyword ":" Nanling, art "," date_time ": "1309791498", "content": "After the competition, Director Kang Yonggang gave a speech. congratulations on the successful conclusion of the competition and on the success of the contestant. "}]}
[Java]
Index. php code
 
<? 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 ("test "));
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 );
}
?>
The Java code in Android is as follows:
 
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.exe cute (httpGet );
HttpRes = client.exe cute (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 ();
}
}
}
}
The database code is as follows:

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,
'Welcome new ',
'Welcome new ',
1309791386, 'xx Emy will hold the XX at XX, XX!
'),

(2,
'Art activity Center ',
'Nan Ling, art ',
1309791498, 'after the game, Director Kang Yonggang gave a speech and congratulated the competition on its successful conclusion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.