I'm using json to read long text from MySQL and set it on the TextView, it works without special characters but as soon as I have text like "an apostrophe' like this one" it returns null on textView. I tried using Html.fromHtml but it doesn't work. Any idea how I can use setText(string_with_special_characters)?
CODE:
public void accessWebService() {
JsonRead task = new JsonRead();
task.execute(new String[] { "http://mysite/myfile.php" });
}
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("json_array");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String chapter = jsonChildNode.optString("Column1");
TextView tvChapter = (TextView) findViewById(R.id.chapter);
tvChapter.setText(Html.fromHtml(chapter));
To answer the question above, I have noticed the following strange behavior:
When I copy text (paragraph) from Microsoft Word and paste into MySQL column, TextView returns null, If I type out the entire paragraph within MySQL column TextView returns the paragraph string as expected. Copying text from Word to Scite or Notepad prior still doesn't work, typing the whole text out works. This is strange as you can't tell if text was copied and pasted or typed out in DB columns, looks exactly the same (including white space).
I can't retype everything... with the info given, please feel free to provide a solution otherwise I will be typing until next year
UPDATE
I have done further investigation:
Note the following apostrophe's
’ '
The one on the left is from text editors while the one on the right is from SQL.
SOLUTION:
UPDATE `mytable`SET `column` = replace(column, "’", "'");
Related
I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}
Hey Guys ive got a problem with my database.
iam displaying my database in a textview looking like:
hh:mm dd:MM:yyyy text
12:14 12.12.2014 awdawdawd
13:12 13:12:2015 awdaw awdw
onclick iam getting the text by:
StringBuilder ortsplit = new StringBuilder();
String item = ((TextView) view).getText().toString();
String[] itemsplit = item.split("\\s+");
String uhrsplit = itemsplit[0].toString();
String datumsplit = itemsplit[1].toString();
ortsplit.setLength(0);
for (int i = 2; i < itemsplit.length; i++) {
ortsplit.append(" " + itemsplit[i].toString());
}
String sortsplit = String.valueOf(ortsplit);
then iam opening my database:
datasource.open();
datasource.findedel(uhrsplit,datumsplit,sortsplit);
datasource.close();
my datasource.findedel:
public void findedel(String pZeit, String pDatum, String pOrt) {
database.delete("TABELLE", "UHRZEIT="+Zeit +"AND DATUM="+Datum+"AND ORT="+Ort,null);
}
ive got no "id" displayed in the rows, earlier it looked like:
1 hh:mm dd:MM:yyyy text
2 12:14 12.12.2014 awdawdawd
3 13:12 13:12:2015 awdaw awdw
and ive just took the "id" and searched my entries for that id = id and deleted the row, but since i deleted the first row i want to search the row by the content.
any1 got a solution for my problem?
You have multiple errors and also you are prone to SQL injection.
You must use prepared statements or you must add quotes to your strings and escaping the quotes the string has, for example, in your code:
database.delete("TABELLE", "UHRZEIT="+Zeit +"AND DATUM="+Datum+"AND ORT="+Ort,null);
this: DATUM="+Datum+"AND is bad coded, there is not space between Datum and AND so, if datum is equal to test, then you string will be like this: DATUM=testAND. That will return syntax errors in mysql, and also string must be quoted like this: DATUM='test' AND.
The main problem of quoting this way is that if Datum has quotes by itself, you will have errors too. For example, if Datum equals to te'st then your string is going to be like this: DATUM='te'st' AND. As you see, you will have 3 quotes and then will return syntax error.
You must read and understand this before going further, because you will end up with a really messy code plenty of errors and vulnerabilities: http://wangling.me/2009/08/whats-good-about-selectionargs-in-sqlite-queries.html
Good luck ;)
And also, in Java all variable names must start in lowercase (Instead of String Datum use String datum)
I have one String and into this string I have a url between two characters # such as "Hello world #http://thisurl# my name is Pippo" I want to take the url (http://thisurl) between two #.
How can I do ? Thanks
String data[] = str.split("#"); //spilliting string and taking into array
ArrayList<String> urlList = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
if(data[i].contains("http://"))
urlList.add(data[i]); //if string contains "http://" it means it is url save int list.
}
now you can get all uls from urlList.get(i) method.
this urlList will give you all the urls available in the string. I dint applied any null or other check. Apply it and try. If want something else try modifying content and checks.
Try String.split(). You really should be trying to google these things first.
here is an example - http://www.java-examples.com/java-string-split-example
The split method divides a string into several strings and store them into an array using a delimiter which can be defined by you.
the second element in the resulting array will be your URL
I have a database thats in the form of a text file, my job is to parse the txt file and display the data in a listview. I have no idea where to start.
Heres an example entry.
"|9251115|,|0|,|DETAILS|,||,||,|Heading Price Text Text |,||,||
Where each || represents a field. There are also html tags between heading price and the text (p,b)
My first idea would be to parse it similarly to an xml document, i.e have it create a new line where it starts with a "|", fill it with everything in between and end the line when it reaches the next "|". But I still have no concrete idea on how to do this.
EDIT:
Taking it one step at a time for now. Using stringtokenizer to read it line by line and remove "," for a start. Ran into a problem, the textview to display the results is displaying false for some reason instead of the scanned text. here's my code if anyone needs a good headscratcher.
Context myContext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView t = (TextView) findViewById(R.id.text);
st = new ArrayList<property>();
try
{
InputStream is;
is = myContext.getAssets().open("rooms.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String read = br.readLine();
while( read != null)
{
StringTokenizer st = new StringTokenizer(read,",");
{
while(st.hasMoreTokens())
{
String a = st.nextToken();
String b = st.nextToken();
String c = st.nextToken();
String d = st.nextToken();
String e = st.nextToken();
String f = st.nextToken();
String g = st.nextToken();
String h = st.nextToken();
t.setText(a+" "+b+" "+c+" "+d+" "+e+" "+f+" "+g+" "+h);
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
While you can definitely implement your own parser using pure Java code, you should consider using a SQLite database (1) to store your data, which will allow you to insert, delete, modify and query much more easily.
If you database comes in that format from an external source, I'd write a one-time parser that parses the data and inserts it into the SQLite database for future use.
Remember that the CPU on Android devices is slower than your average PC CPU, so if you are parsing large amounts of data in this format all the time, your app might become very slow. Hence my suggestion of converting it to a database.
Another option you have in this case is using XML like you said, because there are ready-to-use parsers out there. But the advice about performance remains: you should really avoid reparsing the data all the time and, instead, store it in a ready-to-use format.
(1): http://developer.android.com/reference/android/database/sqlite/package-summary.html
Here is how I would do,
Have an object with getter/setter
Have a list intialized
1) You need to use StreamReaders/Bufferedreader to read the file
2) If each is not empty
2a) Use StringTokenizer to parse the string with "," as delimiter
2b) Set tokenized values to object
2c) Add object to list
3) return the list created in above step.
Note: If large data you need to be careful while reading entire file, you may get OutofMemoryError.
Bruno Oliveira gave very good advice.
You can parse your file by reading it line by line and then use string.split method, as result you will have all your data in an array where you can easily read and put into a list view or move it to a sqlite database.
I'm trying to make a small app (an image gallery from images from the web, were the url's I get from the JSON file that I received). The context of JSON looks like that:
{"images":{
"yXVak":{
"image_hash":"yXVak",
"imgur_page":"http:\/\/imgur.com\/yXVak",
"original_image":"http:\/\/imgur.com\/yXVak.gif",
"large_thumbnail":"http:\/\/imgur.com\/yXVakl.gif",
"small_thumbnail":"http:\/\/imgur.com\/yXVaks.gif",
"message":"I didn't know they made you see THAT well.",
"source":" ",
"date_popular":"2011-07-18 18:45:05"},
.....
I have about 30 more objects that looks like "yXVak".
Now, the problem is, when I'm trying to parse the text, the program can't find the object "yXVak", the exception looks like that: org.json.JSONException: JSONObject["yXVak"] not found.
I parse the JSON file like that:
jObject = new JSONObject(jString);
JSONObject jImages = jObject.getJSONObject("images");
getImages(jImages);
getImages function looks like that:
JSONObject jHash = jImages.getJSONObject("yXVak") ;
String hash = jHash.getString("yXVak");
String page = jHash.getString("http:\\/\\/imgur.com\\/yXVak");
Image[] images = new Image[3];
images[0] = new Image(jHash.getString("original_image"), jHash.getString("http:\\/\\/imgur.com\\/yXVak.gif"));
images[1] = new Image(jHash.getString("large_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVakl.gif"));
images[2] = new Image(jHash.getString("small_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVaks.gif"));
String message = jHash.getString("I didn't know they made you see THAT well.");
String source = jHash.getString(" ");
String date = jHash.getString("2011-07-18 18:45:05");
listOfImages.add(new ImageHash(hash, page, images, message, source, date));
...
By debugging I found that the jString object looks right (the whole string that in the file), but the jImages object missing two first objects ("yXVak", and the second one that I didn't show here "6k9yE").
Can someone help me with that please, what did I do wrong?
I thing you should change this lines in your getImages function:-
JSONObject jHash = jImages.getJSONObject("yXVak");
//Changes in this lines.
String hash = jHash.getString("image_hash");
String page = jHash.getString("imgur_page");
// Rest of your code is same.
Please try this out.I think that it will solve your problem,