Android: how do i parse this mixed up json response - android

The only thing that's guaranteed to always be there is the messagesByDate obj.
The array and objects named such as "15 MAY 2012" are generated by a server(no control) based on rather or not messages are present for that date.
If u notice the first date represented is an array while the other dates are objects containing other objects that have been numbered.
QUESTION 1: how do i parse this without knowing what dates will be present?
QUESTION 2: Some messages are in an array instead of an object. how do I put them all together in one ArrayList. Rather its in an array or not because the array will not always been there.
Please any help would be appreciated as I'm down to my last hair
Thanks.
{
"messagesByDate":{
"15 May 2012":[
{
"id":"1383483367",
"conversation_id":"274618561",
"user_id":"4318264",
"message":"ok will do",
"date_sent":"1337133515",
"date_sent_ago":"7 mins ago"
},
{
"id":"1380222533",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"ok well hmu",
"date_sent":"1337085122",
"date_sent_ago":"13 hrs ago"
},
{
"id":"1380172978",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"superhead",
"date_sent":"1337083910",
"date_sent_ago":"13 hrs ago"
},
{
"id":"1380130860",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"you ready B",
"date_sent":"1337082797",
"date_sent_ago":"14 hrs ago"
},
{
"id":"1378841432",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"hit my cell tho",
"date_sent":"1337054524",
"date_sent_ago":"22 hrs ago"
},
{
"id":"1378836763",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"whats up baby",
"date_sent":"1337054475",
"date_sent_ago":"22 hrs ago"
}
],
"12 May 2012":{
"6":{
"id":"1362948558",
"conversation_id":"274618561",
"user_id":"4318264",
"message":"ok ima text u",
"date_sent":"1336819668",
"date_sent_ago":"3 days ago"
}
},
"11 May 2012":{
"7":{
"id":"1361356267",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"yea thats cool",
"date_sent":"1336790738",
"date_sent_ago":"3 days ago"
},
"8":{
"id":"1357783913",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"sorry im here. would u like to exchange numebers instead?",
"date_sent":"1336722533",
"date_sent_ago":"4 days ago"
},
"9":{
"id":"1357759262",
"conversation_id":"274618561",
"user_id":"5159567",
"message":"hello?",
"date_sent":"1336721851",
"date_sent_ago":"4 days ago"
}
}
}
}
THE ANSWER SORTA-KINDA
JSONObject dateHolder = r.getJSONObject("messagesByDate");
Iterator holderItr = dateHolder.keys();
while(holderItr.hasNext()){
String thisdate = holderItr.next().toString();
Object date = dateHolder.get(thisdate);
if (date instanceof JSONArray) {
System.out.println(thisdate+" is an ARRAY.");
JSONArray jarray = (JSONArray) date;
for(int x=0;x<jarray.length();x++){
String msgId = jarray.getJSONObject(x).getString("id");
String msgConvoId = jarray.getJSONObject(x).getString("conversation_id");
String msgUserId = jarray.getJSONObject(x).getString("user_id");
String msgBody = jarray.getJSONObject(x).getString("message");
String msgDateSent = jarray.getJSONObject(x).getString("date_sent");
String msgDateSentAgo = jarray.getJSONObject(x).getString("date_sent_ago");
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("msgId",msgId);
temp.put("msgUserId", msgUserId);
temp.put("msgBody", msgBody);
temp.put("msgDateSent", msgDateSent);
temp.put("msgDateSentAgo", msgDateSentAgo);
messages.add(temp);
}
} else {
System.out.println(thisdate+" is an OBJECT.");
JSONObject jobj = (JSONObject) date;
Iterator insideDate = jobj.keys();
while(insideDate.hasNext()){
String number = insideDate.next().toString();
System.out.println(number);
String msgId = jobj.getJSONObject(number).getString("id");
String msgConvoId = jobj.getJSONObject(number).getString("conversation_id");
String msgUserId =jobj.getJSONObject(number).getString("user_id");
String msgBody = jobj.getJSONObject(number).getString("message");
String msgDateSent = jobj.getJSONObject(number).getString("date_sent");
String msgDateSentAgo = jobj.getJSONObject(number).getString("date_sent_ago");
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("msgId",msgId);
temp.put("msgUserId", msgUserId);
temp.put("msgBody", msgBody);
temp.put("msgDateSent", msgDateSent);
temp.put("msgDateSentAgo", msgDateSentAgo);
messages.add(temp);
}
}
}
This gives me all the messages in a HashMap and adds it to an ArrayList called messages like I want but its out of order by date. the json is listed by date...anyone know if there is a way to direct json reading? OR are my WHILE and FOR loops out of order? can i sort hashmaps by a key? I'll google that...

First create a class like this:
import java.util.LinkedList;
import android.util.Log;
public class Message{
private LinkedList<String> id = new LinkedList<String>();
private LinkedList<String> conversation_id = new LinkedList<String>();
private LinkedList<String> user_id = new LinkedList<String>();
private LinkedList<String> message = new LinkedList<String>();
private LinkedList<String> date_sent = new LinkedList<String>();
private LinkedList<String> date_sent_ago = new LinkedList<String>();
public LinkedList<String> getId() {
return id;
}
public void setId(String id) {
this.id.add(id);
}
.
.
.
// For checking response after you get info from server
public void printContent() {
for(String str : id)
Log.i("Id>>>", str);
.
.
.
}
}
Then you need to call server in onCreate() add this code:
if(Manager.isOnline(this)) // Check Internet connection and if you find it then
new MyAsyncTask().execute();
Now, you should add this class:
public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
Log.i(TAG, "MyAsyncTask is about to start...");
showProgressBar();
}
#Override
protected Boolean doInBackground(Void... params) {
boolean status = false;
// Get News items in json format
msg = getMessageItems(); // msg is an instance of Message class define it as global variable.
msg.printContent(); // Check result in logcat
if(msg != null)
status = true;
return status;
}
#Override
protected void onPostExecute(Boolean result) {
Log.i(TAG, "MyAsyncTask finished its task. Data returned to caller.");
if(result)
displayData();
hideProgressBar();
}
}
Here we will connect to server, get Json data and parse it.
private Menu getMenuItems() {
Message mMessage = new Message ();
String response = null;
String connection = **YOUR_URL**;
try {
URL url = new URL(connection);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int responseCode = conn.getResponseCode();
Log.i(TAG, "Try to open: " + connection);
Log.i(TAG, "Response code is: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
if (in != null) {
StringBuilder strBuilder = new StringBuilder();
// Read character by character
int ch = 0;
while ((ch = in.read()) != -1)
strBuilder.append((char) ch);
// get returned message and show it
response = strBuilder.toString();
Log.i("JSON returned by server:", response);
JSONObject jObject = new JSONObject(response);
JSONArray contestantObjects = jObject.getJSONArray("**messagesByDate**");
for(int i=0; i<contestantObjects.length(); i++){
mMessage .setId(contestantObjects.getJSONObject(i).getString("id").toString());
// Repeat this to get all of other items
}
}
in.close();
} else
Log.e(TAG, "Couldn't open connection in getMenuItems()");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return mMessage;
}
Now you have an object that each of its item is a list. You can do whatever you want in display method. you can pass it as an object to adapter to show its data.
private void displayData() {
messageAdapter.setData(msg);
listView.setAdapter(messageAdapter);
}

JSONObject json = service.getJunk();
JSONObject msgJson = json.getJSONObject("messagesByDate");
for( Iterator it = msgJson.keys(); it.hasNext(); ) {
Object obj = msgJson.get( (String)it.next() );
if( obj instanceof JSONObject ) {
JSONObject jobj = (JSONObject)obj;
// process json object
} else {
JSONArray arry = (JSONArray)obj;
// process array
}
}

Related

YouTube Api playlist Video restricted to 50. How do I fetch more?

I am trying to fetch videos from a channel's playlist. There are 132 videos but I cant fetch more than 50 videos.
I know that nextPageToken has to be fetched and passed along with url?
This is my first time working with api.
This is how I am able to fetch 50 videos.
Edit
private static String GOOGLE_YOUTUBE_API_KEY = "<API Key>";
private static String CHANNLE_GET_URL="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PL-nbe4FPvDBElyW0Iww5suxJqqmuGBgIH&key="+GOOGLE_YOUTUBE_API_KEY;
public ArrayList<YoutubeDataModel> parseVideoListFromResponse(JSONObject jsonObject) {
ArrayList<YoutubeDataModel> mList = new ArrayList<>();
if (jsonObject.has("items")) {
try {
JSONArray jsonArray = jsonObject.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
if (json.has("id")) {
JSONObject jsonID = json.getJSONObject("id");
String video_id = "";
if (jsonID.has("videoId")) {
video_id = jsonID.getString("videoId");
}
if (jsonID.has("kind")) {
if (jsonID.getString("kind").equals("youtube#video")) {
YoutubeDataModel youtubeObject = new YoutubeDataModel();
JSONObject jsonSnippet = json.getJSONObject("snippet");
String title = jsonSnippet.getString("title");
String description = jsonSnippet.getString("description");
String publishedAt = jsonSnippet.getString("publishedAt");
String thumbnail = jsonSnippet.getJSONObject("thumbnails").getJSONObject("high").getString("url");
youtubeObject.setTitle(title);
youtubeObject.setDescription(description);
youtubeObject.setPublishedAt(publishedAt);
youtubeObject.setThumbnail(thumbnail);
youtubeObject.setVideo_id(video_id);
mList.add(youtubeObject);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return mList;
}
Edit 2:
Added the following but it is not fetching any value.
try {
// String data=null;
JSONObject reader = new JSONObject();
String next_Page_Token = reader.getString("nextPageToken");
Log.d("NextPAgeToken", "NpT"+next_Page_Token);
}
catch (JSONException e)
{
Log.e("Error", "Error is "+e.getMessage());
Toast.makeText(getApplicationContext(),"No next page found", Toast.LENGTH_LONG).show();
}
You need to add one extra query parameter pageToken like below
private static String CHANNLE_GET_URL =
"https://www.googleapis.com/youtube/v3/playlistItems" +
"?part=snippet" +
"&maxResults=50" +
"&playlistId=PL-nbe4FPvDBElyW0Iww5suxJqqmuGBgIH" +
"&key="+GOOGLE_YOUTUBE_API_KEY +
"pageToken=" + NEXT_PAGE_TOKEN;
//Pass like this one
"https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&channelId=UC2LrGJYe_uzI3FBj05BuvLA&key=AIzaSyBlj1dJ9txGcXOOblCJuQ0iwIkhUCgVt1Y&maxResults=50&pageToken=CJYBEAA"
and when you get data, you need to update NEXT_PAGE_TOKEN by fetching value from nextPageToken.
You will get data as
{
"kind": "youtube#searchListResponse",
"etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/MgcKz6rwie5hyKKWdwMChcXzNzU\"",
"nextPageToken": "CJYBEAA",
"prevPageToken": "CGQQAQ",
"regionCode": "IN",
"pageInfo": {
"totalResults": 184,
"resultsPerPage": 50
},
"items": [...]
}
So, convert this data into JSONObject as
JSONObject mainObject = new JSONObject(data);
NEXT_PAGE_TOKEN = mainObject.getString("nextPageToken");
...//Rest your task here
This works fine.

How to parse this wikipedia response?

{
"batchcomplete": "",
"query": {
"pages": {
"25675557": {
"pageid": 25675557,
"ns": 0,
"title": "Cricket",
"extract": "Cricket is a bat-and-ball game played between two teams of eleven players each on a cricket field, at the centre of which is a rectangular 22-yard-long (20 metres) pitch with a target at each end called the wicket (a set of three wooden stumps upon which two bails sit). "
}
}
}
}
this is the code I tried :
public void getJSON(final String city) throws JSONException {
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=" + city);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader reader =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer json = new StringBuffer(1024);
String tmp = "";
while ((tmp = reader.readLine()) != null) {
json.append(tmp).append("\n");
}
reader.close();
data = new JSONObject(json.toString());
if (data.getInt("cod") != 200) {
System.out.println("Cancelled");
return null;
}
} catch (Exception e) {
System.out.println("Exception " + e.getMessage());
return null;
}
return null;
}
#Override
protected void onPostExecute(Void Void) {
if (data != null) {
Log.d("my weather received", data.toString());
try {
//JSONObject forecastJson = new JSONObject(data);
JSONObject forecastArray = data.getJSONObject("query");
System.out.println(forecastArray);
JSONArray pagesArray = forecastArray.getJSONArray("pages");
// JSONArray idArray = pagesArray.getJSONArray(0);
//JSONArray idArray = pagesArray.get(0);
System.out.println(pagesArray);
JSONObject obj = pagesArray.getJSONObject(0);
System.out.println(obj);
//JSONObject weatherarray = data.getJSONObject("pages");
//JSONObject weather = weatherarray.getJSONObject(0);
// final String des = weather.getString("description");
/*for (int i = 0; i < forecastArray.length(); i++) {
JSONObject dailyForecast = forecastArray.getJSONObject(i);
JSONObject tempObject = dailyForecast.getJSONObject("main");
minTemp = tempObject.getDouble("min");
maxTemp = tempObject.getDouble("max");
//add these minTemp and maxTemp to array or the
//way you want to use
}*/
System.out.println("Temp Value : "+" : ");
runOnUiThread(new Runnable() {
#Override
public void run() {
textvw.setText("");
}
});
} catch (Exception e) {
Log.e("GetFeedTask", "Error:" + e.getMessage());
}
}
}
}.execute();
}
The exception is because the response does not contain JSON Array. Change your
JSONArray pagesArray = forecastArray.getJSONArray("pages");
to
JSONObject pagesArray = forecastArray.getJSONObject("pages");
and I believe that you're trying to get keys which are dynamic. You cloud get the objects using JSONObject.getKeys() like below.
Iterator keys = pagesArray.keys();
while(keys.hasNext()) {
String dynamicKey = (String)keys.next();
JSONObject jObj = pagesArray.getJSONObject(dynamicKey);
//Get other attributes by jObj.getString() method.
}
Try and let me know if it works.
The error is clear enough. you try to assign a JSONobject to a JSONArray
JSONArray pagesArray = forecastArray.getJSONArray("pages");
Replace by
JSONObject pagesArray = forecastArray.JSONObject("pages");
the data of a JSONArray are between [] and not {}.
your error is in :
JSONArray pagesArray = forecastArray.getJSONArray("pages");
Your problem is that you getJSONArray while pages are a JsonObject in your data .if your "pages" is a array in your data you must send it in [] from server like this:
{
"batchcomplete": "",
"query": {
"pages": [ {
"pageid": 25675557,
"ns": 0,
"title": "Cricket",
"extract": "Cricket is a bat-and-ball game played between two teams of eleven players each on a cricket field, at the centre of which is a rectangular 22-yard-long (20 metres) pitch with a target at each end called the wicket (a set of three wooden stumps upon which two bails sit). "
},
{
"pageid": 25675557,
"ns": 0,
"title": "Cricket",
"extract": "Cricket is a bat-and-ball game played between two teams of eleven players each on a cricket field, at the centre of which is a rectangular 22-yard-long (20 metres) pitch with a target at each end called the wicket (a set of three wooden stumps upon which two bails sit). "
}
]
}
}
and in android :
try {
//JSONObject forecastJson = new JSONObject(data);
JSONObject forecastArray = data.getJSONObject("query");
System.out.println(forecastArray);
JSONArray pagesArray = forecastArray.getJSONArray("pages");
System.out.println(pagesArray);
for (int k = 0; k < pagesArray.length(); k++) {
try {
JSONObject object = pagesArray.getJSONObject(k);
String pageid = object.getString("pageid");
String ns = object.getString("ns");
String title = object.getString("title");
String extract = object.getString("extract");
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
Log.e("GetFeedTask", "Error:" + e.getMessage());
}

android:how to call method from hyperlink

I am trying to click a hyperlink and call method in android programming...
But the problem is , the link is not showing up and neither the method is getting called. How to achieve this result?
I am basically a javascript/jsp developer, this is my first android application , which i am learning. Accordingly i am trying to click link and call method with parameter....
Results looking like
Java code
private class CallAPI extends AsyncTask<String, String, String> {
private String Content;
#Override
protected String doInBackground(String... params) {
String urlString=params[0]; // URL to call
String resultToDisplay = "";
InputStream in = null;
emailVerificationResult result = null;
// HTTP Get
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
System.out.println("test");
BufferedReader reader = null;
// Get the server response
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
// Append Server Response To Content String
Content = sb.toString();
} catch (Exception e ) {
System.out.println(e.getMessage());
return e.getMessage();
}
/****************** Start Parse Response JSON Data *************/
String OutputData = "<center><b><u>Weight Training</u></b></center><br/><br/>";
JSONObject jsonResponse;
try {
/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse = new JSONObject(Content);
/***** Returns the value mapped by name if it exists and is a JSONArray. ***/
/******* Returns null otherwise. *******/
JSONArray jsonMainNode = jsonResponse.optJSONArray("articleList");
/*********** Process each JSON Node ************/
int lengthJsonArr = jsonMainNode.length();
for (int i = 0; i < lengthJsonArr; i++) {
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
/******* Fetch node values **********/
String name = jsonChildNode.optString("menu_name").toString();
String number = jsonChildNode.optString("id").toString();
String date_added = jsonChildNode.optString("parent_id").toString();
OutputData += " " +
String.format("<a onClick='verifyEmail("+number+","+date_added+")'><b>"+name+"<b> "+ number+" "+ date_added+"</a> ") +"<br/><br/>";
}
/****************** End Parse Response JSON Data *************/
/* Show Parsed Output on screen (activity) */
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
resultToDisplay =OutputData ;
return resultToDisplay;
}
// This is the method that is called when the submit button is clicked
public void verifyEmail(String m,String p) {
String urlString = apiURL + "mid=" + m + "&pid=" + p;
new CallAPI().execute(urlString);
}
protected void onPostExecute(String result) {
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
intent.putExtra(EXTRA_MESSAGE, result);
startActivity(intent);
}
Update:
Instead of link, can i put a button and provide on click method and pass parameter to the method
(Thankfully) You cannot call a function using HTML tags in android. Instead try setting ClickableSpan on you you TextView to get the desired effect
SpannableString ss = new SpannableString("Click Me to do magic");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
doSomeMagic();
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
// apply the clickable span on "Click Me" part which is on index 0 -> 7
// 8 is used because it goes from a -> b-1
ss.setSpan(clickableSpan, 0, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.foo);
textView.setText(ss);

Nothing happens on pressing Menu Item

I am trying to build an app following a tutorial. I have a "Refresh" button that's supposed to populate a ListView (through and ArrayAdaptor)with an array of data from an API call but when I press the "Refresh" button. The button is supposed to run updateWeather() method to load the ListView with some weather data that looks like this (This data appears in my Log actually):
Can someone please help with this. Can't seem to get my head around it.
05-10 23:23:23.921: V/FetchWeatherTask(2037): Forecast entry: Sun May 10 - Clouds - 10/8
05-10 23:23:23.921: V/FetchWeatherTask(2037): Forecast entry: Mon May 11 - Clear - 17/5
05-10 23:23:23.921: V/FetchWeatherTask(2037): Forecast entry: Tue May 12 - Rain - 21/8
05-10 23:23:23.921: V/FetchWeatherTask(2037): Forecast entry: Wed May 13 - Rain - 15/10
05-10 23:23:23.921: V/FetchWeatherTask(2037): Forecast entry: Thu May 14 - Rain - 20/15
05-10 23:23:23.922: V/FetchWeatherTask(2037): Forecast entry: Fri May 15 - Rain - 13/11
05-10 23:23:23.922: V/FetchWeatherTask(2037): Forecast entry: Sat May 16 - Rain - 13/8
ForecastFragment.java
package com.example.siaw.sunshine;
import ...
public class ForecastFragment extends Fragment {
private ArrayAdapter<String> forecastAdaptor;
public ForecastFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh){
updateWeather();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
forecastAdaptor = new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,
R.id.list_item_forecast_textview, new ArrayList<String>());
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(forecastAdaptor);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String forecast = forecastAdaptor.getItem(position);
Intent intent = new Intent(getActivity(), DetailActivity.class)
.putExtra(Intent.EXTRA_TEXT, forecast);
startActivity(intent);
}
});
return rootView;
}
private void updateWeather() {
FetchWeatherTask weatherTask = new FetchWeatherTask();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String location = prefs.getString(getString(R.string.pref_location_key),
getString(R.string.pref_location_default));
weatherTask.execute(location);
}
#Override
public void onStart() {
super.onStart();
updateWeather();
}
public class FetchWeatherTask extends AsyncTask<String, Void, String[]> {
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
/* The date/time conversion code is going to be moved outside the asynctask later,
* so for convenience we're breaking it out into its own method now.
*/
private String getReadableDateString(long time){
// Because the API returns a unix timestamp (measured in seconds),
// it must be converted to milliseconds in order to be converted to valid date.
SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd");
return shortenedDateFormat.format(time);
}
/**
* Prepare the weather high/lows for presentation.
*/
private String formatHighLows(double high, double low) {
// For presentation, assume the user doesn't care about tenths of a degree.
long roundedHigh = Math.round(high);
long roundedLow = Math.round(low);
String highLowStr = roundedHigh + "/" + roundedLow;
return highLowStr;
}
/**
* Take the String representing the complete forecast in JSON Format and
* pull out the data we need to construct the Strings needed for the wireframes.
*
* Fortunately parsing is easy: constructor takes the JSON string and converts it
* into an Object hierarchy for us.
*/
private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays)
throws JSONException {
// These are the names of the JSON objects that need to be extracted.
final String OWM_LIST = "list";
final String OWM_WEATHER = "weather";
final String OWM_TEMPERATURE = "temp";
final String OWM_MAX = "max";
final String OWM_MIN = "min";
final String OWM_DESCRIPTION = "main";
JSONObject forecastJson = new JSONObject(forecastJsonStr);
JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);
// OWM returns daily forecasts based upon the local time of the city that is being
// asked for, which means that we need to know the GMT offset to translate this data
// properly.
// Since this data is also sent in-order and the first day is always the
// current day, we're going to take advantage of that to get a nice
// normalized UTC date for all of our weather.
Time dayTime = new Time();
dayTime.setToNow();
// we start at the day returned by local time. Otherwise this is a mess.
int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);
// now we work exclusively in UTC
dayTime = new Time();
String[] resultStrs = new String[numDays];
for(int i = 0; i < weatherArray.length(); i++) {
// For now, using the format "Day, description, hi/low"
String day;
String description;
String highAndLow;
// Get the JSON object representing the day
JSONObject dayForecast = weatherArray.getJSONObject(i);
// The date/time is returned as a long. We need to convert that
// into something human-readable, since most people won't read "1400356800" as
// "this saturday".
long dateTime;
// Cheating to convert this to UTC time, which is what we want anyhow
dateTime = dayTime.setJulianDay(julianStartDay+i);
day = getReadableDateString(dateTime);
// description is in a child array called "weather", which is 1 element long.
JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
description = weatherObject.getString(OWM_DESCRIPTION);
// Temperatures are in a child object called "temp". Try not to name variables
// "temp" when working with temperature. It confuses everybody.
JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
double high = temperatureObject.getDouble(OWM_MAX);
double low = temperatureObject.getDouble(OWM_MIN);
highAndLow = formatHighLows(high, low);
resultStrs[i] = day + " - " + description + " - " + highAndLow;
}
for (String s : resultStrs) {
Log.v(LOG_TAG, "Forecast entry: " + s);
}
return resultStrs;
}
#Override
protected String[] doInBackground(String... params){
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
if (params.length == 0){
return null;
}
HttpURLConnection urlConnection = null;
//TODO: track and remove.
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
String format = "json";
String units = "metric";
int numDays = 7;
try {
final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
final String QUERY_PARAM = "q";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(UNITS_PARAM, units)
.appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
.build();
URL url = new URL(builtUri.toString());
Log.v(LOG_TAG, "Built URI " + builtUri.toString());
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuilder buffer = new StringBuilder();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
Log.v(LOG_TAG, "Forecast String: " + forecastJsonStr);
} catch (IOException e) {
Log.e("ForecastFragment", "Error", e);
// If the code didn't successfully get the weather data, there's no point in attempting
// to parse it.
return null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("ForecastFragment", "Error closing stream", e);
}
}
}
try {
return getWeatherDataFromJson(forecastJsonStr, numDays);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
//This will only happen if there was an error getting/parsing the forecast.
return null;
}
#Override
protected void onPostExecute(String[] result) {
if (result != null) {
forecastAdaptor.clear();
for (String dayForecastStr : result) {
forecastAdaptor.add(dayForecastStr);
}
// New data is back from the server. Hooray!
}
}
}
}
The data in this ListView is actually not from the API call, the funny thing is I deleted the static String array that's holding THIS data but after cleaning and rebuilding, the static data still appears. I am not sure why this is happening. In the code this data has been removed!
I want to populate this fake data with the data from the API call with the kind of data that shows in LogCat.
It looks like the main issue is that you are not calling notifyDataSetChanged() after modifying the data set in onPostExecute(). This should be all you need to get it working:
#Override
protected void onPostExecute(String[] result) {
if (result != null) {
forecastAdaptor.clear();
for (String dayForecastStr : result) {
forecastAdaptor.add(dayForecastStr);
}
// New data is back from the server. Hooray!
forecastAdapter.notifyDataSetChanged(); //added
}
}
As an alternative, I usually prefer to do all data manipulation on the underlying ArrayList instead of using the methods of ArrayAdapter, see below for details:
Create a ArrayList<String> as a member variable, which will be your data source for the ListView:
public class ForecastFragment extends Fragment {
private ArrayAdapter<String> forecastAdaptor;
private ArrayList<String> data; //added
//.........
Initialize in onCreate():
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
data = new ArrayList<String>(); //added
}
Then, pass in the data source when calling setAdapter():
forecastAdaptor = new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,
R.id.list_item_forecast_textview, data);
Then, in onPostExecute() of your AsyncTask, update the data source, then call notifyDataSetChanged():
#Override
protected void onPostExecute(String[] result) {
if (result != null) {
//forecastAdaptor.clear(); //no need for this
data.clear(); //clear the data source
for (String dayForecastStr : result) {
//forecastAdaptor.add(dayForecastStr);
data.add(dayForecastStr); //update data source
}
// New data is back from the server. Hooray!
forecastAdapter.notifyDataSetChanged(); //added
}
}
See documentation for notifyDataSetChanged()

Android JsonArray into ListView

Im trying to parse json array data into listview!I searched the whole internet and always was on one point! Json aray must have header something like this
"emp_info":[{"employee name":"Adam","employee no":"101700"},{"employee name":"John","employee no":"101701"},{"employee name":"Paul","employee no":"101702"},{"employee name":"Mark","employee no":"101703"},{"employee name":"Donald","employee no":"101704"},{"employee name":"Brain","employee no":"101705"},{"employee name":"Kevin","employee no":"101706"}]}
Where by my understanding the "emp_info" is the header file by which i must search for rest data inside it in android!My College pretending that i can accept and parse the same data into listview without that header name,but every bit of code where i searched to parse json in android was a single line like this!
JSONObject obj = new JSONObject(jsonString);
JSONArray stations = obj.getJSONArray("emp_inf");
where i just have to put the jsonarray header file as you can see in this piece of code!So please help me is that possible to accept json array without this code?because if i try to remove this code i get nullpointer in my code!Will be very happy if you could at least say yes or no!
Posting Full Codes!
Here is the android class which gets the Json and loads it into List View
private class JsonReadTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://54.148.41.171/server/index/dompy");
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
#Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("empployee no");
String number = jsonChildNode.optString("etc.");
String outPut = name + "-" + number;
employeeList.add(createEmployee("data", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "employee no" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}
And by this json array im successfully able to fetch the json which type is the next!
{"emp_info":[{"employee name":"Adam","employee no":"101700"},{"employee name":"John","employee no":"101701"},{"employee name":"Paul","employee no":"101702"},{"employee name":"Mark","employee no":"101703"},{"employee name":"Donald","employee no":"101704"},{"employee name":"Brain","employee no":"101705"},{"employee name":"Kevin","employee no":"101706"}]}
And here is the json array which my college pretends that i must accept!
{"data":"123"}
And im Saying that its not possible to load this json into list view just because it dont have header file like mine the emp_info,but hes saying its not matter i just MUST accept!We just on same project and i just cant understand is it even possible to do what he says?
String name = jsonChildNode.optString("empployee no");
String number = jsonChildNode.optString("etc.");
How can you get the data by "etc." tag I could not understand it firstly. It should return "" instead of employee number.

Categories

Resources