1
我創建了一個Web服務,我想要得到的JSON對象和數據填充到列表視圖的Android 這是我的PHP代碼獲取JSON數據從PHP服務和數據填充到列表視圖的Android
$sql_query="SELECT * FROM testtable";
$query=mysqli_query($con,$sql_query);
$rows = array();
while($r = mysqli_fetch_assoc($query)) {
$rows[] = $r;
}
print json_encode($rows);
這是我的Android代碼,現在我想要得到的JSON對象和數據填充到Android的
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Show(View view){
String method="show";
BackgroundTask backgroundTask=new BackgroundTask(this);
}
}
此列表視圖是第二類的代碼
public class BackgroundTask extends AsyncTask<Void,Void ,String> {
Context ctx;
String result;
AlertDialog alertDialog;
BackgroundTask(Context ctx) {
this.ctx = ctx;
}
protected String doInBackground(Void... Voids) {
URL url = http://hifzil.com/tayyab/select.php;
try {
url = new URL(json_get_settings_url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while((result = bufferedReader.readLine()) != null) {
stringBuilder.append(result+"\n");
}
JSONObject json = new JSONObject(result);
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog=new AlertDialog.Builder(ctx).create();
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Saved")){
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}