MySQL PHPからJSONオブジェクトでAndroidにデータを取得しようとしましたが、私とは連携しません。私は自分の問題を探していましたが、私が見つけた例は私を助けませんでした。jsonでMySQLデータをアンドロイドに取得する方法
私は文字列の配列リストを持っている、私はMySQL DBの文字列を設定します。 その後、JSONで都市の文字列をDBから取得したいのですが、失敗しました。
私の質問は以下のとおりです。
- どのように私は私が街を持っている場合、それが再び表示されないことを確認することができますか?
- Androidのアレイリストで都市を設定するにはどうすればよいですか?
私のPHPコード:
<?php
include 'connection/connection.php';
$noResult = "no results";
// to set the names at the list view
$sql = "SELECT workrCity FROM workersTable ";
$result = $connect->query($sql);
if ($result->num_rows > 0){
while($row[] = $result->fetch_assoc()) {
$json = json_encode($row,JSON_UNESCAPED_UNICODE);
}
} else {
echo $noResult;
}
echo $json;
$connect->close();
?>
私のフラグメントの配列リスト機能は良い作業:
private ArrayList<City> initCities() {
Log.d(TAG, "ArrayList_CitiesFragment_initCities");
String[] cityName = {"","","",""}; // the cities names
ArrayList<City> theCities = new ArrayList<>();
for (String aCityName : cityName) {
City city = new City(aCityName, false);
theCities.add(city);
}
return theCities;
}
を今、私はJSONのような出力でのMySQLから都市名を取得したいです:
handler = new Handler(Looper.getMainLooper());
Thread runner = new Thread(new Runnable() {
@Override
public void run() {
Log.d(TAG, " runner");
GetCitiesJson getCitiesJson = new GetCitiesJson();
try{
String[] res = getCitiesJson.getCitiesDataFromDB();
JSONObject jsonObject = new JSONObject(String.valueOf(res));
JSONObject workrCity = jsonObject.getJSONObject("workrCity");
Activity activity = getActivity();
Toast.makeText(activity,"its :" + workrCity, Toast.LENGTH_SHORT).show();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});
runner.start();
私のコードは正しくないが、何が欠けているか知っている...
アプリケーションがjsonの解析で例外をスローしたくない場合は、 'echo $ noResult'をjson encodeする必要があります。 –