Android getting data containing special/copy-pasted characters from mysql server - android

I need to get some data from a mysql database.I'm running a php script querying the database,json encoding and printing it. Getting the results from my application through a httppost on the php in the server.I am getting all the data from the server except for some entries which contain special/non-typed characters.for eg :“,’
For these entries I am getting null.So other than replacing these characters with typed characters like '"' server side...is there any way I can get these data with these characters to show on the application.
Thanks.
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("cxxn", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("convt", "Error converting result "+e.toString());
}
The result has null values for those data with special characters.Should I change the encoding that I am using..?
Edit:
The MySQL charset is: UTF-8 Unicode (utf8)
<?php
mysql_connect("","","");
mysql_select_db("fdict");
$q=mysql_query("SELECT (Id), (WD), (des), (udte) FROM `dict`");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
This is my server side php code.Have removed the server address,pass.The data with special characters seem to become null only after json_encode().An echo of the sql query result shows proper data without any null values.Someone please help with this.What can i do..?What are my options..?
Thanks.

Solved it Using this.
Needed to put mysql_query('SET CHARACTER SET utf8') before the SELECT query.So that it gets retrived in utf8 format.

Related

how to fix org.json.JSONException: Value <br><table of type java.lang.String

Okay I am new to json and php so youll consider my question simple for you.
I am trying to insert data (name and id) from the app into the database .
and I got this error
org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject
this is my php
<?php
$host='mysql12.000webhost.com';
$uname='a6901827_moudiz';
$pwd='**';
$db="a6901827_justed";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$id=$_REQUEST['id'];
$name=$_REQUEST['name'];
$sql = 'INSERT INTO samle '.
'(id ,name) '.
'VALUES ($id , $name )';
mysql_select_db('a6901827_justed');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
mysql_close($conn);
?>
and this is my code, if you think it needs improvement please don't hesitate.
public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("name",name));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost =new HttpPost("http://justedhak.comlu.com/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
Based on the error, you are trying to pass HTML formatted text in JSON string.
If you expect to pass plain text, this means something is wrong with your PHP script which returns HTML (or it doesn't work as expected).
If you want to pass HTML-formatted text, you can encode your string before adding it to your JSON object.
You are sending data as:
http://justedhak.comlu.com/insert.php?id=x&name=myname
But expecting it to be
{"id": x, "name": "myname"}
which should be in the post body of the http call.
The easiest way would be to forget the JSON for now. Keep the Android code, and remove the JSON part from the PHP.
your reply does not contain JSON as well. So the exception fires at the Android code.

Android: httppost, problems checking result

I'm struggling a bit with checking/verifying the result of a http post operation. I'm checking a database for certain entries, and then converting the result to a JSONArray. However, if the result is null, I don't want to attempt the conversion. I'm catching the exception, so the program doesn't crash if the result is zero, but I'd like to avoid attempting to convert an empty result alltogether.
I'm connecting to the database (via a php script) like this:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringBuilder authentication = new
StringBuilder().append("user").append(":").append("pass");
result = Base64.encodeBytes(authentication.toString().getBytes());
httppost.setHeader("Authorization", "Basic " + result);
nameValuePairs.add(new BasicNameValuePair("date", date));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e(DEBUG_TAG, "Error in http connection" + e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-
8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
As you can see, I'm getting the result from the server and converting it to a string.
Then I wan to convert this string into a JSONArray IF it's not empty. If it's empty, I want to skip it.
However, I cannot figure out how to catch if the converted result string if null or not.
If I do this, after the above code,
itemsview.append("Result: " + result)
then the appended text is "null". Which makes sense, since the returned result from the db (in this test) is blank.
However, if I do this to catch the null result,
if (result.equals("null") {
itemsview.append("Result is null!");
}
the text doesn't appear. So, if I print out the value of the result string, it is null. But when I check it for the value null in the if, then it doesn't turn out to be true.
What am I missing here?
BTW: What I'm doing after this code is, as mentioned, converting the result string to JSONArray, then inserting the values into a List
instead of
if (result.equals("null") {
itemsview.append("Result is null!");}
use
if (result==null) {
itemsview.append("Result is null!");}
I think better you check for result==null rather than what you are doing :) cheers :)

How do I use the command: HttpEntity?

I wish to settle my long term problem by this question and hope you guys would help, but firstly; I have been having issues to connect to a HTTPS self-signed certificate server for almost 3 weeks. Despite the multiple solutions here, I cannot seem to resolve my problem. Probably I did not know how to use it properly or did not have some files or imported the correct libraries.
I came across some websites that requires me to download a certificate from the https site that I am trying to connect into, and when I did that. I have to do the some steps before I can use the certificate or keystore that I created. I got this solution from this website:
Android: Trusting SSL certificates
// Instantiate the custom HttpClient
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
I have a problem, after the last line, as stated above. What do I do with the responseEntity? How do I use it if I wish to display the https website on a WebView? Some help and explanation would be nice :)
If you want the content from the HttpEntity the correct way does not include calling HttpEntity#getContent() to retrieve a stream and doing tons of pointless stuff already available in the Android SDK.
Try this instead.
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
// Retrieve a String from the response entity
String content = EntityUtils.toString(responseEntity);
// Now content will contain whatever the server responded with and you
// can pass it to your WebView using #loadDataWithBaseURL
Consider using WebView#loadDataWithBaseURL when displaying content - it behaves a lot nicer.
You need to call responseEntity.getContent() to get response in InputStream against your requested URL. Use that stream in your way to present data as you want. For example, if the expected data is String, so you may simply convert this stream into string with the following method:
/**
* Converts InputStream to String and closes the stream afterwards
* #param is Stream which needs to be converted to string
* #return String value out form stream or NULL if stream is null or invalid.
* Finally the stream is closed too.
*/
public static String streamToString(InputStream is) {
try {
StringBuilder sb = new StringBuilder();
BufferedReader tmp = new BufferedReader(new InputStreamReader(is),65728);
String line = null;
while ((line = tmp.readLine()) != null) {
sb.append(line);
}
//close stream
is.close();
return sb.toString();
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
return null;
}
InputStream is = responseEntity.getContent();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
String result=sb.toString();
is.close();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
you will have all the content in the String "result"

Setting TextView's text as non-English text received from JSON response

I'm getting back names of (Foursquare) venues from a server call where the names of the venues returned can be in English or non-English.
Assume the venue name is in a JSON object as follows:
{...
"name":"venue name which can be in any language"
...}
I'm creating a JSONObject from this response and then pulling out the name of the venue as follows:
String name = jsonObject.getString("name");
Lastly, I'm setting the TextView's text to show the name of the venue as follows:
myTextView.setText(name);
I'm finding however for Arabic names that where the Arabic characters are joined in the original JSON object (as they should be), the characters that show in the app (i.e. in the TextView) are disjoint. (I'm not too familiar with other languages so can't really tell if they're showing incorrectly too.)
Is there something additional I should be doing to pull out non-English names correctly from the JSON object and setting it as the text of a TextView, or is it down to the phone to decide how the text will be displayed?
Edit: I've tried parsing the server response (as suggested by #bbedward) explicitly specifying the content encoding as UTF-8 as follows...
HttpEntity httpEntity = httpResponse.getEntity();
String responseMessage = EntityUtils.toString(myHttpEntity, "UTF-8");
JSONObject jsonObject = new JSONObject(responseMessage);
... but still no joy. (Arabic characters appear, as before, disjoint in words where they should be joint up.) Could it be a phone thing or is there something extra needing to be done myself to get the words/characters to show proper in non-English languages? Perhaps the server needs to explicitly specify a "Content-Type" header with value "UTF-8"?
I'm going to answer anyway, I'm guessing you aren't getting your json in UTF-8 as i had a similar problem, I believe json won't come any other way.
Complete Example
The only things to concern yourself with this is setting the encoding for the InputStreamReader and creating the JSONObject
private DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://myjsonurl.com/search?type=json");
// Depending on your web service
httppost.setHeader("Content-type", "application/json");
try
{
String result = null;
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
JSONObject myJObject = new JSONObject(sb.toString();
}
catch (Exception e)
{
}
finally
{
try{if(inputStream != null)inputStream.close();}catch(Exception none){}
}
add this line when you connect to mysql:
mysql_set_charset('utf8', $con);
ex:
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_set_charset('utf8', $con);
mysql_select_db(DB_DATABASE);

using Android HttpGet/HttpPost to communicate with odbc database: is there a faster way?

I'm writing an app to use an Android device to get and set data from an Access database on a local area network. I'm using HttpPost and/or HttpGet to communicate with php pages on the server which in turn communicate with the db via odbc.
It works well, but takes almost a second for each query to complete. Is there a faster way?
I'm debugging via usb on the actual device. When I access the php pages from the browser on the device, it's much faster. There is no other traffic on this network.
Thanks!
Edit: Here's the code to retrieve a dataset:
private JSONArray getData(String phpFile,ArrayList<NameValuePair> nameValuePairs){
String result = "";
InputStream is = null;
JSONArray jArray=null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://" + mServerIP + "/dressageNet/"+ phpFile);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONArray(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
nameValuePairs.clear();
return jArray;
}
Create custom server application in whatever you language want and stop using http server and PHP. PHP is slow and this creates overhead. of course you will need to implement your own protocol but it will be a lot faster. I did something like this in java me, and performance was way better than doing POST/GET.

Categories

Resources