Get Hindi data from Apache CouchDB 1.6.1 - java

I am using Apache CouchDB 1.6.1 as my database. We are creating quiz and saving the question data in couchdb. Now, there are users who want to create question in hindi. Data gets saved in couchdb easily but on retrieval of the data from couchdb its get converted into some absurd font. May be there is some issue with the font family and all I am unaware of all this.
We are using Java with Gwt in our project.
public String getData(){
Session session = new Session("192.168.1.70", 5984);
String hindiresult=null;
try{
Database test = session.getDatabase("test");
Document testdoc = test.getDocument("testdoc");
hindiresult=testdoc.getString("hindifield");
}catch(Exception e){
e.printStackTrace();
}
return hindiresult;
}
}
This is our server side code fetching data here and returning it to client on alert. The Image I have already shared.

You need to use utf-8 encoding, to support hindi.

I just changed the encoding of the Eclipse Ide suggested in the following question from default to utf-8. HELPFUL LINK

Related

How to store image from Android app to MySQL

I store the images to SQLite by converting the bitmap to byte array.
Should I do the same thing? Getting the byte array from bitmap, then to JSON, then to PHP, and finally to MySQL.
If yes, how can I do that? I could store strings to MySQL from the app, but couldn't do it on byte arrays.
just convert the byte array into Base64 ... base64 technically is a string, so JSON it to your webservice, there convert it back from Base64 ... and the rest is history
here is a link for converting image to Base64 in android : How to convert a image into Base64 string?
btw, other guys are right it's not so efficient to store the image itself inthe db, storing a reference would be much better ... unless you do not want your images to be right on the sd card, which is something else, security wise !
You can refer to this SO discussion that talks about uploading files to a server using Android.
As a matter of fact it is not recommended to store binary data into relational databases. Refer this SO Discussion. Rather a recommended way would be store the binary data on a server disk location as a file and simply place the path of the file within the database. This would prevent any corruption of data due to discrepancies in the database character set and encodings.
As you are willing to use another solution that is more good, i am explaining you the below procedure.
Instead of storing the image in the database, create a directory specifically for the images, after you successfully upload the image store the path of that image in your database. After that use that path to refer that image.
You can upload image via POST method, and then store reference in the database (ex:- images/img1.bmp)
Whenever you needed you can get file reference using http request(you need to code php for that request handling)
You can access image by using your servers public ip or domain for example : mydomain.com/app1/images/img1.bmp
This is just a one way to do it, so if you think about implementing look for file upload examples via POST
Hope this helps
Use Multipart file upload to send file to your web service (Use libraries like Android Asynchronous Http Client to make the job easy).Then you can encode the file in to Base64 and store in the database as text ,but it is not a best practice to store image files in database, you should save the image as file in the server and keep the path in MySql.
public static String imgToBase64(Bitmap bitmap) {
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
// compress image
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);
out.flush();
out.close();
byte[] imgBytes = out.toByteArray();
return Base64.encodeToString(imgBytes, Base64.DEFAULT);
} catch (Exception e) {
return null;
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Android Login and Registration with PHP, MySQL and SQLite. Can't parse json

I made everything, as here link. But I have an error
Error parsing data org.json.JSONException:
Value n of type java.lang.String cannot be converted to JSONObject
I created MySQL Database and building PHP files on my web hosting.
What is wrong ?
It seems that you have problem with building the JSONObject from incoming stream.
You can try to decode incoming data before "putToJSONObject" with something like this:
try {
incomingMsg = URLDecoder.decode(ipnMsg, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
Maybe I need to use JSONArray ? I found many questions of this problem, and many people advice to use JSONArray. But its very interesting for me, why this example doesn't work. I use russian web hosting, but i check main parameters, like encoded in UTF-8. I can't understand where I must look for a problem. In php files or in java code. Or maybe problem in russian web-hosting.

Create Empty CloudBlockBlob in Azure

I'm hoping the answer to this question is quite simple, but I can't get it working after looking at the Azure Java API documentation.
I am trying to create an empty CloudBlockBlob, which will have blocks uploaded to it at a later point. I have successfully uploaded blocks before, when the blob is created upon the first block being uploaded, but I can't seem to get anything other than ("the specified blob does not exist") when I try to create a new blob without any data and then access it. I require this because in my service, a call is first made to create the new blob in Azure, and then later calls are used to upload blocks (at which point a check is made to see if the blob exists). Is it possible to create an empty blob in Azure, and upload data to it later? What have I missed?
I've not worked with Java SDK so I may be wrong but I tried creating an empty blob using C# code (storage client library 2.0) and if I upload an empty input stream an empty blob with zero byte size is created. I did something like the following:
CloudBlockBlob emptyBlob = blobContainer.GetBlockBlobReference("emptyblob.txt");
using (MemoryStream ms = new MemoryStream())
{
emptyBlob.UploadFromStream(ms);//Empty memory stream. Will create an empty blob.
}
I did look at Azure SDK for Java source code on Github here: https://github.com/WindowsAzure/azure-sdk-for-java/blob/master/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/client/CloudBlockBlob.java and found this "upload" function where you can specify an input stream. Try it out and see if it works for you.

Can I get access to Lotus Notes embedded files without actually extracting them?

I'm working on a way of programatically accessing a Lotus Notes database to gather information on embedded attachments of records over a given period.
My goal is to find records over a given period, then use Apache-POI to get metadata about document size, character count, etc.
The POI part works fine, and so far, I've been able to access the Lotus Notes records thanks to this help:
lotus notes search by date with Java api
and this answer also shows me how to download/copy the attachments:
How do I get all the attachments from a .nsf(lotus notes) file using java
from there I could use my POI code do my job and at the end, just delete the copied attachments. This approach, basically works, but I want to avoid the overhead of copying, saving and then at the end deleting my copy of these attached documents from the database.
I tried passing the result of the EmbeddedObject getSource() method as an input to my POI code and got a FileNotFoundException in the POI code that was expecting a String to make a File.
Is there a way of getting a File reference I can pass to POI, without copying and saving the attachment? Or, what I mean is, is it as simple as getting a File (+path) for the Lotus Notes EmbeddedObject attachment, and how do I do this?
I found the answer and posted it below.
Answering my own question...
...here's the solution I found a little while after posting the question above:
EmbeddedObject's getInputStream to the rescue...
//from the answer in the link in the question above 
Database db = agentContext.getCurrentDatabase();
DocumentCollection dc = db.getAllDocuments();
Document doc = dc.getFirstDocument();
boolean saveFlag = false;
while (doc != null) {
RichTextItem body =
(RichTextItem)doc.getFirstItem("Body");
System.out.println(doc.getItemValueString("Subject"));
Vector v = body.getEmbeddedObjects();
Enumeration e = embeddedObjs.elements();
while(e.hasMoreElements()){
EmbeddedObject eo = (EmbeddedObject)e.nextElement();
if(eo.getType() == EmbeddedObject.EMBED_ATTACHMENT){
//this next line gives Apache-POI access to the InputStream
InputStream is = eo.getInputStream();
POIFSFileSystem POIfs =
HWPFDocument.verifyAndBuildPOIFS(is);
POIOLE2TextExtractor extractor =
ExtractorFactory.createExtractor(POIfs);
System.out.println("extracted text: " + extractor.getText());
is.close(); //closing InputStream
}
eo.recycle(); //recycling EmbeddedObject
//thanks to rhsatrhs for the close() and recycle() tip!

Characterset problem while inserting into mysql database from java application

I have written a application that parses the html code of some web pages. My problem is with inserting that data into my mysq database. So for example i want to insert ľščťžýáíé and when i look into the table i get ?š??žýáíé.
I guess the problem could be that the html pages i'm downloading are encoded in cp1250. but the database is utf8.
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),"cp1250"));
and this is how i download the data.
Do you have some ideas how to fix this problem? Because i allready ran out.
Edit: oh and when i write the data out to the console (with System.out, i know i shouldn't use it... :) ) then every character is showing up correctly.
issue a set names CP1251; just after your connect to mysql and before any inserts
So i found out what works.
As i'm connecting to via JDBC to MySQL i used the following connection string
conString = "jdbc:mysql://"+host+"/"+database+"?useUnicode=true&characterEncoding=utf8";
And this did the trick. I had to force JDBC to use utf8 for the connection using ?useUnicode=true&characterEncoding=utf8

Categories

Resources