Android - Parsing Twitter JSON for image url - java

I'm currently trying to put a twitter feed into my app and currently everything works except when I try to get the image url field from the JSON returned.
Here is my code to parse the JSON:
public ArrayList<Tweet> getTweets() {
String searchUrl =
"http://twitter.com/statuses/user_timeline/vogella.json";
ArrayList<Tweet> tweets =
new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler =
new BasicResponseHandler();
String responseBody = null;
try {
responseBody = client.execute(get, responseHandler);
} catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
Log.e("", "responseBody = " + responseBody);
JSONArray arr = null;
try {
arr = new JSONArray(responseBody);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < arr.length(); i++) {
try {
jsonObject = arr.getJSONObject(i);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Tweet tweet = null;
try {
tweet = new Tweet(
jsonObject.getString("profile_image_url"),
jsonObject.getString("text"),
jsonObject.getString("created_at")
);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tweets.add(tweet);
}
return tweets;
}
And here is the error I get:
02-14 00:19:18.672: W/System.err(809): org.json.JSONException:JSONObject["profile_image_url"] not found.
Despite the "profile_image_url" being present - click the link to see the JSON - LINK. Everything else in the feed appears to be retrievable so why cant I get the image url?

Your jsonObject variable refers to the top level array element of your response, which contains elements like in_reply_to_status_id, geo, etc. The profile_image_url property is not a property in that top level array element, but rather a child element of the user property.
[
{
"in_reply_to_status_id":null,
"in_reply_to_user_id_str":null,
...
"geo":null,
"user":
{
"profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/112136794\/twilk_background_4c1620bca9ed3.jpg",
...
}
},
...
]
To access the profile_background_image_url, you would have to do something roughly like the following:
JSONObject userObject = jsonObject.getJSONObject ("user");
String url = userObject.getString("profile_image_url");

here is the cause:
[
{
"user":{
"profile_image_url":"http:\/\/a3.twimg.com\/profile_images\/1249241027 \/LarsVogel10_normal.png",
},
"created_at":"Mon Feb 13 22:34:09 +0000 2012",
"text":"Fun evening at speaker dinner at #jfokus."
},
{
..
...
profile_image_url is under "user", while "text" and "created_at" are above

I'm PRETTY sure that to do this you have to read the JSON from the URL that has the 'profile_image_url' variable. The twitter.com/statuses/ URL doesn't have that variable I don't think. This shows that the 'profile_image_url' is at /profile_images/. Just letting you know that changing your URL to that might be able to find it. :P

Related

Asynctask doesnt work after too many rows retrieved

I am doing an app where I synchronize my online DB to the offline DB everytime the user logs in. The table is dropped in offline, recreated then new rows gets added ( Its neccessary to drop it and add new instead of just checking and adding the rows that are not in the table already). I had about 200 rows in my online table and they are synchronised to my offline table relatively fast (in the background, then I tried 3000 and it was still processing. But When I generated 90 000 rows and tried to synchronize it to my offline DB it wouldnt move.
The log in onPreExecute() executed, but none of the logs in my doInBackground. json is not null.
For each retrieved row I am adding a row in offline.
Anyone know what could be the issue?
I tried adding LIMIT 200 in my PHP Scripts and still didnt do it, which was weird, cause when I had 200 rows it executed, but when I limit the output to 200 it does not.
Thank you for any answers, that would bring me closer to the solution.
public class SyncVykresToOffline {
String DataParseUrl = "/scriptsforandroidapplicationofflinemode/SyncVykresToOffline.php";
JSONObject json = null;
String str = "";
HttpResponse response;
DBHelper dbh;
private Context mContext;
public static boolean syncedvykres = false;
int k = 200;
public SyncVykresToOffline(Context context) {
mContext = context;
dbh = new DBHelper(mContext);
}
public class SyncVykres extends AsyncTask<Void, Void, Void>
{
public Context context;
public SyncVykres(Context context)
{
this.context = context;
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
Log.i("Poradie_zacal","ano");
}
#Override
protected Void doInBackground(Void... arg0)
{
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost(DataParseUrl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("limit", String.valueOf(k)));
try {
myConnection.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
HttpResponse response = myClient.execute(myConnection);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int i = 0;
try{
int vykres_version;
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(i);
Log.i("Poradie_json",String.valueOf(jArray.length()));
String
Nazov_vykresu;
int Version,
ID_vykres,
ID_stav,
ID_zakazka,
Poradie;
if(json == null) {
Log.i("Poradie","son is null");
}
while(json != null) {
Log.i("Poradie","been here");
ID_vykres = Integer.parseInt(json.getString("ID_vykres"));
vykres_version = dbh.getVykresVersion(ID_vykres);
Nazov_vykresu = json.getString("Nazov_vykresu");
ID_stav = Integer.parseInt(json.getString("ID_stav"));
ID_zakazka = Integer.parseInt(json.getString("ID_zakazka"));
Version = Integer.parseInt(json.getString("Version"));
Poradie = Integer.parseInt(json.getString("Poradie"));
Log.i("Poradie",json.getString("Poradie"));
dbh.SyncVykresToOffline(new technicky_vykres(ID_vykres,Nazov_vykresu,ID_stav,ID_zakazka,Version,Poradie));
i++;
json = jArray.getJSONObject(i);
}
} catch ( JSONException e) {
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result)
{
syncedvykres = true;
}
}
}
Edit: added Logcat logs.
06-25 20:36:07.013 8278-8308/com.example.chris.normitapplication W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.example.chris.normitapplication.offline.SyncVykresToOffline$SyncVykres.doInBackground(SyncVykresToOffline.java:102)
at com.example.chris.normitapplication.offline.SyncVykresToOffline$SyncVykres.doInBackground(SyncVykresToOffline.java:44)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
Edit 2: added PHP Script from where the JSON array is retrieved from:
<?php
include 'DatabaseConfig.php';
$conn = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"utf8");
$vykres = array();
$sql = "SELECT * FROM `technicky_vykres`";
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
$conn->close();
?>
Issue identified when logging STR:
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>openresty</center>
</body>
</html>
My hunch is, its the data that is breaking the loop and causing your program to end without going through the complete data set. I just added simple try/catch (see below) to printout any data objects, that we can't parse and but still continue to process the next row. Of course you'll need to have better error handling in place for production quality code.
while(json != null) {
try{
Log.i("Poradie","been here");
ID_vykres = Integer.parseInt(json.getString("ID_vykres"));
vykres_version = dbh.getVykresVersion(ID_vykres);
Nazov_vykresu = json.getString("Nazov_vykresu");
ID_stav = Integer.parseInt(json.getString("ID_stav"));
ID_zakazka = Integer.parseInt(json.getString("ID_zakazka"));
Version = Integer.parseInt(json.getString("Version"));
Poradie = Integer.parseInt(json.getString("Poradie"));
Log.i("Poradie",json.getString("Poradie"));
dbh.SyncVykresToOffline(new technicky_vykres(ID_vykres,Nazov_vykresu,ID_stav,ID_zakazka,Version,Poradie));
i++;
json = jArray.getJSONObject(i);
}catch(Exception e){
//Something wrong with the data? log it and see if you can find the culprit row/data....
Log.i("The faulty jason obj: " + json);
continue; //Move on the next one....
}
}
The error indicates that JSONArray believes your response is not formed as a Json Array.
Here's the code within JSONArray throwing your error:
public JSONArray(JSONTokener readFrom) throws JSONException {
/*
* Getting the parser to populate this could get tricky. Instead, just
* parse to temporary JSONArray and then steal the data from that.
*/
Object object = readFrom.nextValue();
if (object instanceof JSONArray) {
values = ((JSONArray) object).values;
} else {
throw JSON.typeMismatch(object, "JSONArray");
}
}
So the tokener does not recognize the object as a JSONArray. I would take a look at your raw response and see if adding a limit doesn't change the response to be an object with a result array inside of it (so that it can also include arguments to help web calls handle paging). Either way, there's something in the format of the response that the tokener does not recognize as being a Json array.
Upon realizing that the PHP script stopped working because too many rows & columns were retrieved instead of * I only Selected data that I truly needed (about 1/3 of all columns), then I added a where clause where ID would be above the number I sent from post and I keep repeating the script until finally the response is not "null".
Thank you for everyone who contributed to finding the solution.

Handle array data from server android

in my application data coming from a server in the form of an array,
i cant handle the data i will share my code please help me.
JSONObject jsonObject = new JSONObject(response);
String name = jsonObject.getString("status");
String name1 = name.trim();
if (name1.equals("success")) {
Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
try {
JSONArray array = jsonObject.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
jsonObject = array.getJSONObject(i);
s_key = jsonObject.getString("initKey");
s_iv = jsonObject.getString("initIv");
sec_url = jsonObject.getString("url");
s_init_hash = jsonObject.getString("initHash");
}
} catch (JSONException e) {
e.printStackTrace();
}
There is no JSONArray
JSONObject jsonObject = new JSONObject(response);
String name = jsonObject.getString("status");
String name1 = name.trim();
if (name1.equals("success")) {
Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
try {
JSONObject jsonObjectData = jsonObject .getJSONObject(i);
s_key = jsonObjectData.getString("initKey");
s_iv = jsonObjectData.getString("initIv");
sec_url = jsonObjectData.getString("url");
s_init_hash = jsonObjectData.getString("initHash");
}
} catch (JSONException e) {
e.printStackTrace();
}
You Response is JSONObject not in JSONArray check it
FYI
{ } brackets means JSONObject
[ ] brackets means JSONArray
Parse your json like this
JSONObject jsonObject = new JSONObject(response);
String name = jsonObject.getString("status");
String name1 = name.trim();
if (name1.equals("success")) {
Toast.makeText(getApplicationContext(), "inside", Toast.LENGTH_LONG).show();
try {
JSONObject jsonObjectData = jsonObject.getJSONObject("data");
s_key = data.getString("initKey");
s_iv = data.getString("initIv");
sec_url = data.getString("url");
s_init_hash = data.getString("initHash");
} catch (JSONException e) {
e.printStackTrace();
}
}
data Is not Json Array cause it start with {} it`s Json Object
Json Array start with []
So you need To use
SONObject jsonObjectData = jsonObject .getJSONObject(i);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
String name = jsonObject.getString("status");
String name1 = name.trim();
if (name1.equals("success")) {
JSONObject jsonObjectData = jsonObject.getJSONObject("data");
s_key = jsonObjectData.getString("initKey");
s_iv = jsonObjectData.getString("initIv");
sec_url = jsonObjectData.getString("url");
s_init_hash = jsonObjectData.getString("initHash");
}
} catch (JSONException e) {
e.printStackTrace();
}
response
{
"status": "success",
"data": {
"initKey": "abc",
"initHash": "cde",
"initIv": "efg",
"versionNo": "123 ",
"url": "https://www.youtube.com"
}
}
Code
try {
JSONObject outerJsonObject=new JSONObject(response);
String status=outerJsonObject.getString("status");
if(status.equals("success"))
{
JSONObject innerJsonObjectData=outerJsonObject.getJSONObject("data");
String initKey =innerJsonObjectData.getString("initKey");
String initHash =innerJsonObjectData.getString("initHash");
String initIv =innerJsonObjectData.getString("initIv");
String versionNo =innerJsonObjectData.getString("versionNo");
}
} catch (JSONException e) {
e.printStackTrace();
}
Comments
/*
{
//outerJsonObject
"status":"success",
"data": // innerJsonObjectData
{
"initKey":"abc",
"initHash": "cde",
"initIv": "efg",
"versionNo": "123 ",
"url": "https://www.youtube.com"
}
}
*/
When you are working with JSON data in Android, you would use JSONArray to parse JSON which starts with the array brackets. Arrays in JSON are used to organize a collection of related items (Which could be JSON objects).
For example:
[{"name":"item 1"},{"name": "item2} ]
On the other hand, you would use JSONObject when dealing with JSON that begins with curly braces. A JSON object is typically used to contain key/value pairs related to one item. For example:
{"name": "item1", "description":"a JSON object"}
JSONObject jsonObject = new JSONObject(response);
String name = jsonObject.getString("status");
String name1 = name.trim();
if (name1.equals("success")) {
Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
try {
JSONObject data = jsonObject. getJSONObject("data");
s_key = data.getString("initKey");
s_iv = data.getString("initIv");
sec_url = data.getString("url");
s_init_hash = data.getString("initHash");
}
} catch (JSONException e) {
e.printStackTrace();
}

Return JSONObject with JSONArray not working in Amazon Lambda Java function

I created one Java lambda function and deploy that function to Amazon API gateway.
I want to return JSONObject with inner JSONArray.
But I got { } empty JSONObject in response.
If I set jsonobjetc.toString() in response, That will work perfectly.
But if I return JSONObject I will return empty {} JSON response.
Am I missing something?
JSONObject mainJsonObject;
#Override
public Object handleRequest(Object input, Context context) {
inputHashMap = (LinkedHashMap) input;
responseJSON = new ResponseJSON();
mainJsonObject = new JSONObject();
saveDataToDynamoDB(inputHashMap);
return mainJsonObject;
}
public void saveDataToDynamoDB(LinkedHashMap inHashMap){
String login_id = (String) inputHashMap.get("login_id");
String first_name = (String) inputHashMap.get("first_name");
String last_name = (String) inputHashMap.get("last_name");
try{
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());
Table tableUserDetails = dynamoDB.getTable(USER_PROFILE_TABLE);
Item userProfileTableItem = new Item().withPrimaryKey("login_id", login_id)
.withString("first_name", first_name).withString("last_name", last_name);
tableUserDetails.putItem(userProfileTableItem);
mainJsonObject.put("status", "Success");
mainJsonObject.put("message", "Profile saved successfully.");
mainJsonObject.put("login_id", login_id);
mainJsonObject.put("first_name", first_name);
mainJsonObject.put("last_name", last_name);
}catch(Exception e){
try {
mainJsonObject.put("status", "Failed");
mainJsonObject.put("message", "Failed to saved profile data.");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
In case you are calling this code from HTTP request of a web service & catch the response result in your application, then your response should be in String format (String representation of JSON object).
After you get the response in String format, then parse the JSON string to JSON Object & do your further logic.

Trying to return a value from Java and read it in JavaScript

I have a method in Java that will be returning a JSON object and I am trying to get it to load in to some JavaScript. Here is my code but it is not working:
Java:
public JSONObject ticketListener(TicketMessage message) {
JSONObject listener = new JSONObject();
try {
listener.put("Ticket Messenge", message.getText());
listener.put("Ticket Number", message.getTicketNumber());
listener.put("Operation", message.getOperation());
listener.put("UserID", message.getUserID());
listener.put("Operation Description", message.getOperationDescription());
} catch (JSONException e) {
e.printStackTrace();
}
}
JavaScript:
$scope.ticketListener = function() {
var val = $entry(#com.cmcflex.flex.gui.programs.projectmanager.gui.messenger.ticketListener::getValue())();
};
Please help.

Permissions Error - Trying to get friends using android facebook sdk

I am trying to add a feature to my android app that allows users to "checkin" with other people tagged to the checkin.
I have the checkins method working no problem and can tag some one by adding the user ID as a parameter (see code below)
public void postLocationTagged(String msg, String tags, String placeID, Double lat, Double lon) {
Log.d("Tests", "Testing graph API location post");
String access_token = sharedPrefs.getString("access_token", "x");
try {
if (isSession()) {
String response = mFacebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("access_token", access_token);
parameters.putString("place", placeID);
parameters.putString("Message",msg);
JSONObject coordinates = new JSONObject();
coordinates.put("latitude", lat);
coordinates.put("longitude", lon);
parameters.putString("coordinates",coordinates.toString());
parameters.putString("tags", tags);
response = mFacebook.request("me/checkins", parameters, "POST");
Toast display = Toast.makeText(this, "Checkin has been posted to Facebook.", Toast.LENGTH_SHORT);
display.show();
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
} catch(Exception e) {
e.printStackTrace();
}
}
This works fine (I've posted it in case it is of help to anyone else!), the problem i am having is i am trying to create a list of the users friends so they can select the friends they want to tag. I have the method getFriends (see below) which i am then going to use to generate an AlertDialog that the user can select from which in turn will give me the id to use in the above "postLocationTagged" method.
public void getFriends(CharSequence[] charFriendsNames,CharSequence[] charFriendsID, ProgressBar progbar) {
pb = progbar;
try {
if (isSession()) {
String access_token = sharedPrefs.getString("access_token", "x");
friends = charFriendsNames;
friendsID = charFriendsID;
Log.d(TAG, "Getting Friends!");
String response = mFacebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("access_token", access_token);
response = mFacebook.request("me/friends", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
} catch(Exception e) {
e.printStackTrace();
}
}
When i look at the response in the log it reads:
"got responce: {"error":{"type":"OAuthException", "message":"(#200) Permissions error"}}"
I have looked through the graphAPI documentation and searched for similar questions but to no avail! I'm not sure if i need to request extra permissions for the app or if this is something your just not allowed to do! Any help/suggestions would be greatly appreciated.
You might need the following permissions:
user_checkins
friends_checkins
read_friendlists
manage_friendlists
publish_checkins
Check the related ones from the API docs. Before that, make sure that which line causes this permission error and try to fix it.
The solution is to implement a RequestListener when making the request to the Facebook graph API. I have the new getFriends() method (see below) which uses the AsyncGacebookRunner to request the data.
public void getFriends(CharSequence[] charFriendsNames,String[] sFriendsID, ProgressBar progbar) {
try{
//Pass arrays to store data
friends = charFriendsNames;
friendsID = sFriendsID;
pb = progbar;
Log.d(TAG, "Getting Friends!");
//Create Request with Friends Request Listener
mAsyncRunner.request("me/friends", new FriendsRequestListener());
} catch (Exception e) {
Log.d(TAG, "Exception: " + e.getMessage());
}
}
The AsyncFacebookRunner makes the the request using the custom FriendsRequestListener (see below) which implements the RequestListener class;
private class FriendsRequestListener implements RequestListener {
String friendData;
//Method runs when request is complete
public void onComplete(String response, Object state) {
Log.d(TAG, "FriendListRequestONComplete");
//Create a copy of the response so i can be read in the run() method.
friendData = response;
//Create method to run on UI thread
FBConnectActivity.this.runOnUiThread(new Runnable() {
public void run() {
try {
//Parse JSON Data
JSONObject json;
json = Util.parseJson(friendData);
//Get the JSONArry from our response JSONObject
JSONArray friendArray = json.getJSONArray("data");
//Loop through our JSONArray
int friendCount = 0;
String fId, fNm;
JSONObject friend;
for (int i = 0;i<friendArray.length();i++){
//Get a JSONObject from the JSONArray
friend = friendArray.getJSONObject(i);
//Extract the strings from the JSONObject
fId = friend.getString("id");
fNm = friend.getString("name");
//Set the values to our arrays
friendsID[friendCount] = fId;
friends[friendCount] = fNm;
friendCount ++;
Log.d("TEST", "Friend Added: " + fNm);
}
//Remove Progress Bar
pb.setVisibility(ProgressBar.GONE);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
Feel free to use any of this code in your own projects, or ask any questions about it.
You can private static final String[] PERMISSIONS = new String[] {"publish_stream","status_update",xxxx};xxx is premissions

Categories

Resources