I have problems when submitting data from an RSS in my App - java

First of all I apologize for the translation, I speak Spanish.
My problem is the following:
I use the RSS of my own website to show data in my App, the bad thing is that they do not show up immediately, I need you to do it as it is necessary.
I think the problem may be in the XMLParser class:
public class XMLParser {
private URL url;
private Context contextor;
public XMLParser(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
#SuppressLint("SimpleDateFormat")
public void parse() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.url.openConnection().getInputStream());
Element root = dom.getDocumentElement();
NodeList itema = root.getElementsByTagName("item");
for (int i = 0; i < 1; i++) {
Node item = itema.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("content:encoded")) {
Log.i("XMLParser", "GUARDO " + property.getFirstChild().getNodeValue());
int star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg0\">"));
int fiinal = (property.getFirstChild().getNodeValue().indexOf("</table>"));
String cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
MyBus.setDomincal(cadena);
star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg1\">"));
String parteCadena = (property.getFirstChild().getNodeValue().substring(star));
fiinal = parteCadena.indexOf("</table>");
cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
Log.i("XMLParser", "GUARDO " + url);
Log.i("XMLParser", "GUARDO " + cadena);
//Log.i("XMLParser", "GUARDO " + MyBus.getIntermedio());
MyBus.setIntermedio(cadena);
star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg2\">"));
parteCadena = (property.getFirstChild().getNodeValue().substring(star));
fiinal = parteCadena.indexOf("</table>");
cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
MyBus.setZodiacal(cadena);
star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg3\">"));
parteCadena = (property.getFirstChild().getNodeValue().substring(star));
fiinal = parteCadena.indexOf("</table>");
cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
MyBus.setExtraordinario(cadena);
//MyBus.setSeguro(1);
}
}
}
} catch (Exception e) {
//MyBus.setSeguro(1);
//Esto solo es para lanzar un error y para que la app haga un cierre forsozo
//throw new RuntimeException(e);
}
}}
I do not know what I'm doing wrong, because when I check the link in the browser it is updated. Please, if someone can help me, I would really appreciate it.
An example would be: on the website I create a post or simply edit one, I change certain data that I have or there, let's say it said 'red' but now I edited it and put it 'blue'. In the link that I see of the RSS in the browser appears updated, but if I go to my app keeps saying red or does not appear the new post that I created until several hours later or the next day, and that is wrong because the information given my application must be updated every so often and it does not do me any good to do it many hours later.
At first I thought it could be the webview cache but the problem comes before it was shown in that webview.
Thanks in advance.

Related

Clarifai This API call was not sucessful

I was trying some concepts with Clarifai locally using java, from some images I have on my computer. The idea was to get 30 concepts for each of the images which would then be saved as tags in the application I am developing. Initially the code was working and I printed the result for each one of the images in the folder I'm reading from, but a little more than an hour ago I left the following error:
Below I leave the java code I was using to get the concepts of my images:
protected static String getConcepts(String path, String API_KEY) {
/*Vou obter 30 conceitos para uma determinada imagem*/
String result = "";
ClarifaiClient client = new ClarifaiBuilder(API_KEY).client(new OkHttpClient()).buildSync();
final List<ClarifaiOutput<Concept>> predictionResults = client.getDefaultModels().generalModel().predict().
withInputs(ClarifaiInput.forImage(new File(path))).withMaxConcepts(5).executeSync().get();
Iterator<ClarifaiOutput<Concept>> resultTags = predictionResults.iterator();
TreeMap<String, Float> tags = new TreeMap<>();
int numberOfTags;
if (resultTags.hasNext()) {
ClarifaiOutput<Concept> next = resultTags.next();
numberOfTags = next.data().size();
for (int j = 0; j < numberOfTags; j++) {
Concept concept = next.data().get(j);
String name = concept.name();
tags.put(concept.name(), concept.value());
}
}
for (Map.Entry<String, Float> entry : tags.entrySet()) {
result += entry.getKey() + "," + entry.getValue() + ",";
}
System.out.println( result);
return result;
}
public static void main(String[] args) throws IOException {
/*ClarifaiClient client = new ClarifaiBuilder("b5bc3cb6b7ba4a8cbd6d950c811c18b3").buildSync();
final List<ClarifaiOutput<Concept>> response =
// You can also do client.getModelByID("id") to get your custom model
client.getDefaultModels().generalModel()
.predict()
.withInputs(ClarifaiInput.forImage("https://samples.clarifai.com/metro-north.jpg"))
.executeSync()
.get();
System.out.println(response);*/
//String src = "/home/claudia/Desktop/EmotionROI/images/anger/selected_images/";
//String img = "12.jpg";
//String dest = src+"teste"+img;
//Lista de ficheiros do diretório - obtenção dos ficheiros
String pathDir = "/home/claudia/Desktop/EmotionROI/images/anger/selected_images/";
File imageFile = new File(pathDir);
File[] listFiles = imageFile.listFiles();
//System.out.println("Os meus ficheiros são: ");
String resultF = "";
List<String> listTagImg = new ArrayList<>();
//ciclo que vai permitir aceder ao nome dos ficheiros
for (int i = 0; i < listFiles.length; i++) {
String nameFiles = listFiles[i].getName();
//System.out.println(nameFiles);
//criação de um pah novo com o nome de cada um dos ficheiros
String pahtFiles = pathDir+nameFiles;
listTagImg.add(pahtFiles);
}
List<String> listTags = new ArrayList<>();
String line="";
for (int j = 0; j < listFiles.length; j++) {
//System.out.println("lista"+listTagImg.get(j));
line += getConcepts(listTagImg.get(j), "b5bc3cb6b7ba4a8cbd6d950c811c18b3");
}
//getConcepts(listTagImg.get(), "241b3315671f4baeaec399186c435022");
System.out.println(line);
What am I doing roung?
This (the illegal reflective access Warning) can happen in older versions of protobuf. What version are you using? Are you able to update?
The API should be using 3.12.0, but I've seen instances where an older installed version was used unexpectedly.
Just saw that the error message you're getting is actually NoSuchElementException so the Clarifai network error is probably not related to an outage or network issues like I originally suspected.
Would be helpful to know what line 134 is in your getConcepts function to see if it is passing something odd to the Clarifai API which might be choking on it.

Inserting multiple videos to a playlist using multithreading results in videos missing

When I insert the videos serially, all the videos get inserted to the playlist but it takes a long time. When I use multithreading more than half of the videos are missing in the end. How can I insert multiple videos quickly without losing any videos?
// Insert videos. 5 videos per asyncTask
List<List<YTVideo>> chunks = Lists.partition(videos, 5);
for (int i = 0; i < chunks.size(); i++) {
videoAndPlaylistContainer container = new videoAndPlaylistContainer();
container.playlistId = playlistId;
List<YTVideo> chunk = chunks.get(i);
container.videos = chunk;
InsertPlayListItemTask insertPlaylistItemsTask = new InsertPlayListItemTask();
runningTasks.add(insertPlaylistItemsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,container));
}
.
private class InsertPlayListItemTask extends AsyncTask<videoAndPlaylistContainer, Void, Void>{
#Override
protected Void doInBackground(videoAndPlaylistContainer... params) {
videoAndPlaylistContainer container = params[0];
List<YTVideo> videosChunk = container.videos;
String playlistId = container.playlistId;
for (int i = 0; i < videosChunk.size(); i++) {
YTVideo video = videosChunk.get(i);
String videoId = video.getId();
long pos = video.getPosition();
try {
ResourceId resourceId = new ResourceId();
resourceId.setKind("youtube#video");
resourceId.setVideoId(videoId);
PlaylistItemSnippet playlistItemSnippet = new PlaylistItemSnippet();
// playlistItemSnippet.setTitle("First video in the test playlist");
playlistItemSnippet.setPlaylistId(playlistId);
playlistItemSnippet.setResourceId(resourceId);
playlistItemSnippet.setPosition(pos);
PlaylistItem playlistItem = new PlaylistItem();
playlistItem.setSnippet(playlistItemSnippet);
YouTube.PlaylistItems.Insert playlistItemsInsertCommand =
youtube.playlistItems().insert("snippet", playlistItem);
playlistItemsInsertCommand.execute();
System.out.println("Inserted video: " + video);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
edit: videos.size() to chunks.size(). Removed insertedCount++;
This line doesn't look right...
for (int i = 0; i < videos.size(); i++) {
The i variable will grow based on the size of the videos collection, but you only ever use the i variable to access an element within the chunks collection here...
List<YTVideo> chunk = chunks.get(i);
I can't imagine why you don't (eventually) get an ArrayIndexOutOfBounds exception, except maybe you're testing your code (so far) with fewer than 5 videos.

JSONObject.toString() returns OutOfMemoryError

I have an Android app.
First, the app do sync process. In this process, the server sends to the device an JSON object as String by which it can build the available questionnaires.
GetQuestionnairesResponse.java:
public class GetQuestionnairesResponse extends ResponseHandler
{
public GetQuestionnairesResponse(String result, AsyncRequest request)
{
super(result, request);
}
#Override
public void handleResponse()
{
DataSyncActivity caller = (DataSyncActivity) request.getCaller();
BackgroundManager bckMng = BackgroundManager.getInstance(caller);
PreferencesManager preference = PreferencesManager.getInstance(null);
boolean status = true;
int numOfWrongJsonVer = 0;
int totalNumOfQuestionnaires = 0;
// Handle data from server
// Creating JSON Parser instance
try
{
QuestionnaireDataSource questionnaireDS = new QuestionnaireDataSource(caller);
questionnaireDS.open();
JSONArray jArr = new JSONArray(result);
JSONObject j = null;
totalNumOfQuestionnaires = jArr.length();
for (int i = 0; i < jArr.length(); i++)
{
j = jArr.getJSONObject(i);
long questId = j.getLong("questionnaireId");
long surveyId = j.getLong("surveyId");
String questName = j.getString("name");
String desc = j.getString("description");
int version = j.getInt("questionnaireVersion");
int jsonVersion = j.getInt("jsonVersion");
if (jsonVersion == PreferencesManager.jsonVersion)
{
// Save the pages part
String filename = questId + "_" + version + "_" + jsonVersion + ".json";
HelpFunctions.writeJSON(filename, j.toString());
Questionnaire quest = questionnaireDS.createQuestionnaire(questId, questName, desc, surveyId, version, jsonVersion, filename);
if (quest == null)
throw new RuntimeException("Cant save the questionnaire: " + questName);
}
else
{
numOfWrongJsonVer ++;
}
}
questionnaireDS.close();
}
catch (Exception e)
{
status = false;
if (e.getMessage() != null)
Log.e(TAG, e.getMessage());
}
caller.setInSync(false);
...
}
The result i get from the server i parse it to Json array.
The result in some cases can bee 3 megabytes.
The solution I found was to add an attribute in manifest.xml:
android:largeHeap="true"
It solved the problem. I don't know why but the problem returned again in the last day.
I will be happy to get suggestions how to solve the problem.
The problem is that the json object not parsed as expected so it
If the JSON was originally 3 MB and you call toString() on the JSONObject parsed from it, the JSONObject is still taking up memory, plus it's going to need to do a 3 MB allocation to hold the String. You may not have that much memory available.
But the thing about OutOfMemoryError is that the allocation that uses up the last bit of RAM isn't necessarily to blame for there being so little RAM available. Big allocations are just more likely to be the thing that pushes it over the edge. It's likely that you have a memory leak somewhere else in your app, and you'll need to use a tool like Eclipse MAT to find it.

Jacob Export Outlook Contact Pictures (Java, Jacob)

Good Morning,
I’am using Jacob 1.17 o read all my Outlook Contact Pictures and save them to an File. The Procedure works pretty fine for the first 199 Contatcs. After that the Dispatch.call fails and terminates with the following Exception:
Exception in thread "main" com.jacob.com.ComFailException: Invoke of: SaveAsFile
Source: Microsoft Outlook
Description: Cannot save the attachment. Cannot create file: ContactPicture.jpg.
Right-click the folder you want to create the file in, and then click Properties on
the shortcut menu to check your permissions for the folder.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.callN(Dispatch.java:453)
at com.jacob.com.Dispatch.call(Dispatch.java:541)
at outlookStuff.ManageContactsOutlook.tmpTest(ManageContactsOutlook.java:217)
at mainPackage.Main.main(Main.java:32)
I’m really not sure way. I tested a different set of Contacts – same Error. Set all Objects to null to make shore that the Garbage Collector is involved but it doesn’t help.
The piece of Code which makes the trouble:
public void tmpTest(int intOutlookFolder, String strWorkingDir) {
Dispatch dipNamespace = this.axc.getProperty("Session").toDispatch();
Dispatch dipContactsFolder = Dispatch.call(dipNamespace, "GetDefaultFolder", (Object) new Integer(intOutlookFolder)).toDispatch();
Dispatch dipContactItems = Dispatch.get(dipContactsFolder, "items").toDispatch();
#SuppressWarnings("deprecation")
int count = Dispatch.call(dipContactItems, "Count").toInt();
for (int i=1; i<=count; i++) {
Dispatch dipContact;
dipContact = Dispatch.call(dipContactItems, "Item", new Integer(i)).toDispatch();
String strEntryID = Dispatch.get(dipContact, "EntryID").toString().trim();
//For Testing
Status.printStatusToConsole("Outlook Contact "+strEntryID+" loaded");
byte[] byteContactPicture = null;
String strPathToTmpPicture = null;
Dispatch dipAttachments = Dispatch.get(dipContact, "Attachments").toDispatch();
#SuppressWarnings("deprecation")
int countAttachements = Dispatch.call((Dispatch) dipAttachments, "Count").toInt();
for (int j=1; j<=countAttachements; j++) {
Dispatch currentAttachement;
currentAttachement = Dispatch.call(dipAttachments, "Item", new Integer(j)).toDispatch();
if (Dispatch.get(currentAttachement, "FileName").toString().equals("ContactPicture.jpg")) {
strPathToTmpPicture = strWorkingDir+strEntryID+".jpg";
//The Crashing Part
Dispatch.call(currentAttachement, "SaveAsFile", strPathToTmpPicture);
File tmpFile = new File(strPathToTmpPicture);
if (tmpFile.exists()) {
try {
byteContactPicture = org.apache.commons.io.FileUtils.readFileToByteArray(tmpFile);
} catch (IOException e) {
e.printStackTrace();
}
}
currentAttachement = null;
tmpFile = null;
}
currentAttachement = null;
}
dipAttachments = null;
}
dipContactItems = null;
dipContactsFolder = null;
dipNamespace = null;
}
May someone has an idea?
Thanks
Aviation

Getting MediaEntryModule as null while fetching RSS feeds. Unable to fetch media content

I am getting MediaEntryModule as null. Because of that i am unable to fetch Media content like Image, previewImage,Video,audio details.
While debugging i found the Module implementation in SyndEntry is using the following:
DCModuleImpl.interface=interface com.sun.syndication.feed.module.DCModule
DCModuleImpl.uri=http://purl.org/dc/elements/1.1/
MediaEntryModule mediaModule = (MediaEntryModule) entry.getModule(MediaEntryModule.URI);
mediaModule is coming as null.
Jars used in the java 6 code are.
rome-1.0.jar
rome-modules-1.0.jar
I found the answer.
The only way to read the rss feeds if not getting through rome API is. You need to get the ulr of that feed news as shown below:
com.sun.syndication.feed.synd.SyndEntry entry = //get from RSS Url using rome api
String link=entry.getLink();
URL res = new URL(url);
Document doc = Jsoup.connect(url).get();
URL res = new URL(url);
Document doc = Jsoup.connect(url).get();
URL res = new URL(url);
Document doc = Jsoup.connect(url).get();
Element head = doc.head();
Elements titleTags = head.getElementsByTag("title");
if (!titleTags.isEmpty()) {
title = titleTags.text();
} else {
Elements metaTitle = head.select("meta[property=og:title]");
title = metaTitle.attr("content");
if (metaTitle.isEmpty()) {
Elements metaTitle = head.select("meta[name=twitter:title]");
title = metaTitle.attr("content");
}
}
String description = "";
Elements metaDescription = head.select("meta[name=description]");
if (!metaDescription.isEmpty()) {
description = metaDescription.attr("content");
} else {
Elements metaDescription = head.select("meta[property=og:description]");
if (!metaDescriptionFacebook.isEmpty()) {
description = metaDescriptionFacebook.attr("content");
} else {
metaDescription = head.select("meta[name=twitter:description]");
description = metaDescription.attr("content");
}
}
return description;
String previewUrl = "";
Elements metaImage = head.select("link[rel=image_src]");
if (!metaImage.isEmpty()) {
previewUrl = metaImage.first().absUrl("href");
} else {
Elements metaImage= head.select("meta[property=og:image]");
if (!metaImageFacebook.isEmpty() && StringUtils.isNotBlank(metaImage.attr("content"))) {
previewUrl = metaImage.first().absUrl("content");
} else {
Elements metaImage = head.select("meta[name=twitter:image]");
if (!metaImage.isEmpty() && StringUtils.isNotBlank(metaImage.attr("content"))) {
previewUrl = metaImage.first().absUrl("content");
}
}
}
You can get more content like video, audio,etc from Doc element.

Categories

Resources