私は、Android Studioのログインに成功し、レジスタシステムとアプリケーションを設計しました。私はhosting24で自分のDBをホストしています。表示テーブルやcolums
私は、DBからデータを取得し、アプリケーション内で画面上に表示する必要があります。
どのようにすることができますか?私はこのアプリケーションのために書かれたコードのヒープを持っていますので、私が投稿するのを見るためにはどのコードが必要かを示唆しています。私は、先生は、彼らが登録した学生とこれらの学生に関連し、対応するデータの一覧が表示されますアプリケーションにログインしている場合
シナリオだろう..私はここに投稿する必要がありますどのようなコードあまりにもわかりません。ここで
<?php
$con = mysqli_connect("host", "username", "pw", "db");
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];
$statement = mysqli_prepare($con, "SELECT * FROM Student");
mysqli_stmt_bind_param($statement, "ss",$FirstName, $LastName);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["FirstName"] = $FirstName;
$response["LastName"] = $LastName;
}
echo json_encode($response);
?>
私のJavaコードは、この中で
public class UserAreaActivity extends AppCompatActivity implements View.OnClickListener{
Button fetch;
TextView text;
EditText et;
HttpURLConnection urlConnection = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_area);
fetch= (Button) findViewById(R.id.fetch); //XML Button to get the data
fetch.setOnClickListener(this);
}
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(UserAreaActivity.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Fetching data...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL("MY PHP URL");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
// ambil data dari Json database
try {
JSONArray Jarray = new JSONArray(result);
for(int i=0;i<Jarray.length();i++)
{
JSONObject Jasonobject = null;
Jasonobject = Jarray.getJSONObject(i);
//get an output on the screen
String firstName = Jasonobject.getString("FirstName");
String db_detail="";
if(et.getText().toString().equalsIgnoreCase(firstName)) {
db_detail = Jasonobject.getString("detail");
text.setText(db_detail);
break;
}
}
this.progressDialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
結果であり、それは私のためにデータをフェッチでハングです。私はちょうど
<?php
// Code to connect to database
// fetch and process the data
// print json echo json_encode($output)
?>
あなたは、データベースからデータをフェッチし、(あなたが好む場合、または他のフォーマット)JSONとしてそれを投稿することができますサーバー側のファイルを作成する必要がありますまず
ありますが、このためにAsyncHttpClientライブラリを使用する必要はありません。多くの選択肢があります(改造は、JSONの安らかなAPIについては、IMOを使用する方が簡単です)。 – zapl
正解、それはそれを行う方法の一つです – PRYM
コメントありがとうございます。誰かが私のコードがなぜぶら下がっているのか教えていただけますか? –