我刚遇到一个问题,当我从我的服务器读取php到android应用程序时,而不是输出“šinko”,而是输出“Å¡inko”。 但是当我调用“hhttp.website/data.php”(php的名称)时,我得到了正确的输出。但是从我的Android应用程序中读取它时会出现某种混乱...... 你知道我该怎么办? 我的java代码;
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";
}
?>
答案 0 :(得分:0)
您需要将标头设置为PHP文件
header('Content-type: text/plain; charset=utf-8');
然后您需要将数据转换为base64编码
base64_encode($result)
当您在Android中获得响应时,您首先需要将编码后的字符串转换为现在解码您进入textview
的特殊字符,
用于Android解码
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");