i am creating a snipet of code for an app that takes a json string and returns an ArrayList. i did one verson with gson and an trying to do it using straight json. but when running the json part i keep getting cant find local variable 'feedArray'\ it only happen for the jsonToFeedItem method
my code is
any ideas,
thanks
my code is
Code:
package ca.sbmgroup.codechallengelarryseymour;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
[USER=1021285]@override[/USER]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json = "[" +
"{" +
"'Id':1," +
"'username':'test user'," +
"'body':[" +
"{" +
"'Id':3," +
"'bodyType':'text'," +
"'text':'Hello World'" +
"}," +
"{" +
"'Id':4," +
"'bodyType':'image'," +
"'mediaLocation':'http://.../'" +
"}" +
"]" +
"}," +
"{" +
"'Id':2," +
"'username':'test user'," +
"'body':[" +
"{" +
"'Id':4," +
"'bodyType':'video'," +
"'mediaLocation':'http://.../'" +
"}" +
"]" +
"}" +
"]";
//gsonToFeedItem(json);
jsonToFeedItem(json);
}
public String getFeed(){
return "...";//returns a json array of json objects representing the feed items.
}
public void displayFeed(ArrayList<FeedObject> feed)
{
}
public ArrayList<FeedObject> gsonToFeedItem(String jsonString){
ArrayList<FeedObject> feedList = new ArrayList<>();
FeedObject[] feedArray = new Gson().fromJson(jsonString, FeedObject[].class);
feedList = new ArrayList<FeedObject>(Arrays.asList(feedArray));
return feedList;
}
public ArrayList<FeedObject> jsonToFeedItem(String jsonString){
try {
ArrayList<FeedObject> feedList = new ArrayList<>();
JSONArray jsonArray = new JSONArray(jsonString);
for (int i =0; i< jsonArray.length();i++)
{
List<body> bodyList = new ArrayList<>();
JSONObject feedJsonObject = jsonArray.getJSONObject(i);
int feedId = feedJsonObject.getInt("Id");
String userName = feedJsonObject.getString("username");
FeedObject feedObject = new FeedObject(feedId,userName);
JSONArray bodyArray = feedJsonObject.getJSONArray("body");
for(int r =0; r< bodyArray.length(); r++)
{
body bodyObject = null;
JSONObject bodyJsonObject = bodyArray.getJSONObject(r);
int bodyId = bodyJsonObject.getInt("id");
String bodyType = bodyJsonObject.getString("bodyType");
String text = null;
URL mediaLocation = null;
if(bodyType.equals("text")){
text = bodyJsonObject.getString("text");
bodyObject = new body(bodyId, bodyType,text);
}
else {
mediaLocation = new URL(bodyJsonObject.getString("mediaLocation"));
bodyObject = new body(bodyId, bodyType, mediaLocation);
}
bodyList.add(bodyObject);
}
feedObject.setBodyList(bodyList);
feedList.add(feedObject);
}
return feedList;
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException me) {
me.printStackTrace();
}
return null;
}
}
any ideas,
thanks
Last edited by a moderator: