0

I just came across one problem, that when I am reading php from my server to android applicaiton, instead of ouput "šinko" there is ouput "Å¡inko". But when I am calling "hhttp.website/data.php"(name of php) I get correct output. but when reading it from my android application there is some kind of a mess... Do you have any idea what should I do? My java code;

public class Admin extends AppCompatActivity {

private TextView test;
String text;
List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);
    test = (TextView) findViewById(R.id.test);
    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    String type = "data";

    try {
         text = backgroundWorker.execute(type).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    list = new ArrayList<>(Arrays.asList(text.split("/")));
    for(String x : list){
        System.out.println(x);
    }

}

}

 <?php
require "conn.php";
$mysql_query = "SELECT
    Name,
    Surname,
    Name_of_fee,
    sum(Cost) as dept
FROM Person
JOIN Relation ON Person.ID = Relation.Person_ID
JOIN Fee ON Fee.ID = Relation.Fee_ID
GROUP BY Person.ID;";
$result = mysqli_query($conn, $mysql_query);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "". $row["Name"]." " . $row["Surname"]. " " . $row["dept"]."<br>" ;
    }
} else {
    echo "0 results";
}
?>
K.Os
  • 5,123
  • 8
  • 40
  • 95
Swanson
  • 103
  • 1
  • 1
  • 6
  • It seems related to charset. Are you using same in both side, like UTF-8? – ramires.cabral Aug 22 '17 at 20:41
  • How to change them? – Swanson Aug 22 '17 at 20:46
  • In Java String has this constructor: public String(byte bytes[], String charsetName). Maybe this solve. In PHP I've no idea. – ramires.cabral Aug 22 '17 at 20:50
  • I am not sure if this is somehow connected but when I use System.out.println("Š")! It goes through just fine. – Swanson Aug 22 '17 at 20:54
  • Yes. Because you're creating a string directly in code. Trying creating a string using constructor I mentioned and set a charset != UTF-8 and println this string. – ramires.cabral Aug 22 '17 at 20:57
  • Character encoding can be confusing. Get a hex viewer, make sure you are setting proper UTF-8 encoding... Can see what is happening... *c5a1...c2a1* – ficuscr Aug 22 '17 at 21:01
  • 1
    The problem is that while the string maybe be properly encoded UTF8 it is being decoded/displayed as ISO8859-1. Fix your display encoding. https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Sammitch Aug 23 '17 at 00:08

1 Answers1

0

You need to set header into your PHP file

header('Content-type: text/plain; charset=utf-8');

then after you need to convert your data into base64 encoding

base64_encode($result)

when you get a response in android you first need to convert you encoded a string to decode now your special characters you get into your textview, for the Android decoding

byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
Kishan Donga
  • 2,851
  • 2
  • 23
  • 35
  • 1
    `then after you need to convert your data into base64 encoding`. Nonsense. Then you should write your utf-8 text. – greenapps Aug 23 '17 at 07:00