Я новичок в android. Я хочу получить значение моего идентификатора с помощью json на моем php сервере, чтобы я мог получить все данные, которые мне нужны, из моей базы данных.
Это запрос, который я хочу сделать
$query1=mysqli_query($con,"SELECT * FROM jadwal_matakuliah WHERE nim_mhs ='$id'");
мой php код
<?php
include "koneksi.php";
$username = $_POST["id"];
$query1=mysqli_query($con,"SELECT * FROM jadwal_matakuliah WHERE nim_mhs ='$id'");
if($query)
{
while($row=mysqli_fetch_array($query))
{
$flag[]=$row;
}
print(json_encode($flag));
}
mysqli_close($con);
?>
мой android код
String id, username;
SharedPreferences sharedpreferences;
public static final String TAG_ID = "id";
public static final String TAG_USERNAME = "username";
String urladdress="http://10.0.2.2/multi/menu_mhs.php";
String[] name;
String[] jam;
String[] email;
String[] imagepath;
ListView listView;
BufferedInputStream is;
String line=null;
String result=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
listView=(ListView)findViewById(R.id.lview);
StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
collectData();
CustomListView customListView=new CustomListView(this,name,email,imagepath,jam);
listView.setAdapter(customListView);
// getSupportActionBar().setTitle("MHS");
txt_id = (TextView) findViewById(R.id.txt_id);
txt_username = (TextView) findViewById(R.id.txt_username);
btn_logout = (Button) findViewById(R.id.btn_logout);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
id = getIntent().getStringExtra(TAG_ID);
username = getIntent().getStringExtra(TAG_USERNAME);
txt_id.setText("" + id);
txt_username.setText("( " + username +" )");
}
private void collectData() {
//Connection
try {
URL url = new URL(urladdress);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
is = new BufferedInputStream(con.getInputStream());
} catch (Exception ex) {
ex.printStackTrace();
}
//content
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
//JSON
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
name = new String[ja.length()];
jam = new String[ja.length()];
email = new String[ja.length()];
imagepath = new String[ja.length()];
for (int i = 0; i <= ja.length(); i++) {
jo = ja.getJSONObject(i);
name[i] = jo.getString("nama_matakuliah");
email[i] = jo.getString("dosen");
imagepath[i] = jo.getString("photo");
jam[i] = jo.getString("jadwal");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}