Android gets news data from the server and displays it on the client

Source: Internet
Author: User
Tags response code

News Client Case

The first time to enter the news client needs to request the server to get news data, do a listview display,
In order to open the news client for the second time to quickly display the news, you need to cache the data in the database, the next time you can go directly to the database to get the news directly to show.

Overall steps:
1. Write Layout listview OK

2. Locate the ListView and set the entry's Click event. Ok

3. Get the data available to the ListView to make a presentation.

3.1: Get the local database cached news data and let the ListView display. If there is no network, the empty interface is not displayed.
3.2: The request server gets the news data, which is a JSON string that needs to parse the JSON and encapsulate it into the list collection. Provided to the ListView display.

public static String Newspath_url = "xxxx";
Encapsulates false data from the news to the list returned
public static arraylist<newsbean> Getallnewsfornetwork (context context) {
arraylist<newsbean> ArrayList = new arraylist<newsbean> ();
try{
1. Request the server for news data
Gets a URL object that obtains a Urlconnnection object from a URL object
URL url = new URL (newspath_url);
HttpURLConnection connection = (httpurlconnection) url.openconnection ();
Setting the way and time-out for connections
Connection.setrequestmethod ("GET");
Connection.setconnecttimeout (10*1000);
Get Request Response Code
int code = Connection.getresponsecode ();
if (code = = 200) {
Gets the requested stream information
InputStream InputStream = Connection.getinputstream ();
String result = streamutils.streamtostring (InputStream);

2. Parse the obtained news data into the list collection.

Jsonobject Root_json = new Jsonobject (result);//encapsulates a string into a JSON object.
Jsonarray Jsonarray = Root_json.getjsonarray ("Newss");//Get Root_json in Newss as Jsonarray object

for (int i = 0; i < jsonarray.length (); i++) {//Loop traversal Jsonarray
Jsonobject News_json = Jsonarray.getjsonobject (i);//Get JSON for a piece of news

Newsbean Newsbean = new Newsbean ();

Newsbean. id = news_json.getint ("id");
Newsbean. Comment = News_json.getint ("comment");//Comment number
Newsbean. Type = News_json.getint ("type");//news type, 0: Headline 1: Entertainment 2. Sports
Newsbean. Time = news_json.getstring ("Time");
Newsbean. des = news_json.getstring ("des");
Newsbean. title = news_json.getstring ("title");
Newsbean. News_url = news_json.getstring ("News_url");
Newsbean. Icon_url = news_json.getstring ("Icon_url");

Arraylist.add (Newsbean);

}

3. Clear the old data in the database and cache the new data in the database
New Newsdaoutils (context). Delete ();
New Newsdaoutils (context). Savenews (ArrayList);
}

}catch (Exception e) {
E.printstacktrace ();
}
return arrayList;
}

3.3: After obtaining the server data successfully, it needs to be cached to the local database, and the local database needs to be emptied before caching.

4. Create a adapter inheritance Baseadapter, encapsulate 4 methods, need to receive the news data obtained


5. Set the adapter to the ListView.

Android gets news data from the server and displays it on the client

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.