Greek characters - phpMyAdmin - android

I send some data (utf8) in my dataBase and I'm seeing them from phpMyAdmin. The data contains greek and english characters. The problem is that greek characters are showing up as question marks(????). The english are ok. No matter what I have tried:
For dataBase and my column collation: utf8_unicode_ci, utf8_general_ci
After my connection with dataBase:
mysqli_query ("SET NAMES 'UTF8'", $dbc);
mysqli_query ("SET CHARACTER SET 'UTF8'", $dbc);
the problem still remains and I can't figure out how to solve it. Does somebody knows how to fix that?
Update:
The data are sent from an Android application:
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("a", "hello, ΓΕΙΑ ΣΟΥ" ));
nameValuePairs.add(new BasicNameValuePair("b", "Good Morning, ΚΑΛΗΜΕΡΑ"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
httpclient.execute(httppost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (IOException e) {
Log.i("HTTP Failed", e.toString());
}
And the command to insert values into database:
$q= 'INSERT INTO `table`(`x`, `y`) VALUES ("'.$_POST["a"].'","'.$_POST["b"].'")';

Check that your db has UTF8 encoding enabled for the db/tables
Check on browser that UTF8 is enabled (e.g. for Firefox: View/Character Encoding/Unicode (UTF-8))
Check reponse Content-type http header:
Content-Type: text/html; charset=utf-8

This is a FAQ so the phpMyAdmin team has written this page to help:
http://wiki.phpmyadmin.net/pma/Garbled_data

I had the same problem, I searched and read this post.
After I have found a solution to my problem I want to share it.
change this line of your code
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
to
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
and now you are ok.

George, at first I thought that your code was in PHP, taking into consideration that you mentioned phpMyAdmin. Although, I do have a little experience in Android, and I would avoid writing Greek characters (or any non-standard characters) in a .java file.
Instead, you should use the strings.xml file that Android supports, and access your strings in the code like this:
R.string.kalimera
where kalimera is the name of the variable you use for your "Hello" text.
That would perfectly explain why the database is filled with question-marks (????) instead of the actual characters (or at least so I think).

Related

Parameter from android not recognized in API

Goal
I am writing a program where a user's input is taken as a parameter and queried against an online API.
Problem
Oddly, I cannot get my parameter into my API successfully. The error I get is
"Could not look up user information; You have an error in your SQL syntax;" Which as it says plainly , is an SQL error. Therefore I was thinking there was a problem in passing my parameter since the application works when I hard code parameter and say "select name from table where id=1".
This is the parameter code and despite many edits and changes I got the same issue which caused me to look to my php even if everything works right in the browser.
HttpParams param = new BasicHttpParams();
ArrayList<NameValuePair> inputArguments = new ArrayList<NameValuePair>();
inputArguments.add(new BasicNameValuePair("id", idnum));
HttpClient client = new DefaultHttpClient(param);
HttpPost request = new HttpPost("http://myurl.com/DAIIS/getName.php");
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setEntity(new UrlEncodedFormEntity(inputArguments, "UTF-8"));
HttpResponse httpResponse = (HttpResponse) client.execute(request);
Where I think the problem lie
I belives the problem lies in my select statement
<?php
header("Content-Type:application/json");
//Connect to DB
include ("dbcon.php");
//Run query
$para=$_GET['id'];
$sql=("SELECT name FROM class where stu_id=$para");
I say this because after stripping my API to the bare minimum the program's error was Could not look up user information; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
but if i hard code the parameter (it works) or put something random like stu_id=$_GET['id']; it returns blank.
So is the way that I used this parameter incorrect for android? even if it works in the browser?
Thank you
As you asked for :
Just change '$_GET' to '$_POST',
As a side note
You can also check 'POST' request in browser, in order to do that add 'Rest client plugin' to your browser and you are done and have fun with api calls :)

Steps to save data to a DB in sql server from android app?

I have connected my database from sql server to my android app and get an output and everything runs fine.I just need help with a register page where I want to save data that has been entered into edittext boxes tothe database but cant seen to get the correct method for this. Any help or guide lines would be very much appreciated thanx :-D
Have a look at DBHelper class, Hope it solves your problem
Oh, forgot I had some files on my mail :).
Here is a little example:
Java (android):
nameValuePairs.add(new BasicNameValuePair("Username","%"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("location php file");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Php:
mysql_query("INSERT INTO Persons (UserName) VALUES '".$_REQUEST['Username']."'");
I thought it was something like this. You may need to change it a little, but you have a start here.

Android – Is it possible to make an Http-Post with non-String parameters?

I am implementing an android app in which I want to use some methods from a server (which was not implemented by me). Now when I try to make an http-post where I have to pass only String parameters everything works fine with a code like:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user[email]", email));
nameValuePairs.add(new BasicNameValuePair("user[password]", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
JSONObject response=new JSONObject(responseBody);
System.out.println("RESPONSE " + response.toString());
I get the response as a json object which I can easily use to take the attributes I wish.
Now there are methods that require non String values (integer, or boolean) as parameters. I cannot pass these arguments in a list such as List<NameValuePair> since this takes only Strings. I tried to pass it as a json object too but no success.
So my first question is if it is possible to have non String parameters in http post? And if yes, how should it be done? Eg if in the code above email was an integer and password a Boolean (in shake of the example), how should I handle them?
Thank you all in advance!
Sure, consider a file upload.
The file is binary (ok, consider uploading a picture if someone consider a text not to be binary enough)
the technique is a http-post
All http request parameters will come in as strings, but the server side code can convert them. The server could for example grab a JSON string from a request parameter and turn it into an object that contains any amount of serialized data. This could include integers, lists etc.
The implementation though will be dependent on that server side code. Both the client and server for example could use GSON to send objects and lists back and forth.
public void doPost(...)
{
String param = request.getParameter("someParam");
MyCustomObject myCustomObject = (MyCustomObject)gson.fromJson(param, MyCustomObject.class);
}

Json.org Android adding weird characters

I have a client / server app written using Android and I'm using the standard org.json package classes bundled with android to do the parsing and creating.
I've been getting weird characters appearing on the server side right in the middle of the json strings generated for example (not the full one, because its big):
{!lo":"es_MX","id":2791884,"os":"8"}
As you can see the (!) exclamation mark appears randomly instead of a double quote. I also get other random characters appearing mid string. It's very bizarre.
Here is the code which creates the JSON object...
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", someValue);
Here is the code which sends..
HttpPost type = new HttpPost(<server url here>);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("v", jsonObject.toString()));
type.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpClient.execute(type); // This is a DefaultHttpClient
I say random, but the exclamation mark in this exact position is consistent in many errors, but not every-time. About 5 messages that get this error, among tens of thousands per day. And usually not the contents of the values inserted into the json, but the characters (such as the quote character above) that define the structure of the message, which suggests to me that this isn't a character set issue.
Has anyone come across this?
it seems you are composing string in other format, and on receiving text decode in another format
like iso to utf.
It looks like your sender is not properly setting the character set. Spanish will have symbols not present in regular ASCII or most Windows encodings, you need to use UTF-8:
Content-Type: text/html; charset=utf-8
Without knowing which HTTP exchange you're using (read more), it is not possible to give you an exact code snippet to fix the problem - but that should be easy enough to figure out.
You give not enough information. Radical method to fix your problem is just replace all (!) characters to (").
string.replaceAll("!", "\"");
I guess it is server side issue.
I had also simmilar problem. Let me write much more to describe my environment. My server was returning data in json format. But my problem was connected with special chars like ąść. YOu should know, json_encode() will return from server in this case string text as a null.
I know, it sucks! So I added mysql_query('SET CHARACTER SET utf8'); before my selction for items from database. This allowed me to take strings from server with special diacritics letters.
Now on the app site, I was taking data from server by GET method. First I was storing result data into InputStream. Then I was packing it into InputStreamReader and byte by byte I was appending it into stringBuilder. That's ready appended text was converting by toString() ready string. Then I was putting it to new JsonArray(readyString). However I discovered some parts of text for json had weird chars.. Especially in that places where were special letters like żóć. For example "description":"aaa" was throwing "descriptionPffa":"aa"null:`.
I decided to try another way for converting result from data. In places where I was converting data from server I used method below. At the end, wgen I got byteArrayOutputStream object I changed it to new String(byteArray) and then somehow it worked with new JsonArray(new String(byteArray))!
public class Streams {
public static byte[] getBytes(InputStream is) {
int len;
int size = 1024;
byte[] buf = new byte[0];
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
buf = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return buf;
}
}
Print the json on the client (using Log.d or similar) and see if it contains weird characters before sending it to the server.

Android httppost with httpget

First of all. Sorry for my bad enghlish.
I want to write a program what can communicate with my website with post/get parameters but when i run the progress httppost with http://xyz.xyz/?server&user=this&pass=that
it's not work... how can i fix this problem if i can do that.
the sintax
GET
username
password
POST
message
captcha
I know exists two method for this but i don't know one progress for these two method with together.
(httppost / httpget methods; but together... i need it)
Thank you for helping
I did not fully understand your question, but if you want to login to a website (HTTP POST and all), the below link might help.
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
Also, note, the "stringdata" and "id" given in the example is the id of the input parameter in the html code.
<input id="___THIS HERE___" value="..etc" >
According to your question, I'd say you do it like this:
...
nameValuePairs.add(new BasicNameValuePair("user", "this"));
nameValuePairs.add(new BasicNameValuePair("pass", "that"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
...

Categories

Resources