我想将此数据解析为 dataread.php
并想通过 roll no... 从 PHP 服务器获取一些数据
我想从 PHP 检索数据并将其显示在 android 的 TextView 中。我在下面写了一些代码,但它不起作用。
请帮帮我。提前感谢您的支持。
public class MainActivity extends AppCompatActivity(
TextView result;
EditText roll;
Button search;
Editable data;
private ProgressDialog pd;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (Button) findViewById(R.id.button);
roll = (EditText) findViewById(R.id.rollnumber);
result = (TextView) findViewById(R.id.textView);
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("loading");
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
data = roll.getText();
getSqlDetails();
}
});
}
private void getSqlDetails(){
String url = "http://192.168.0.1/data/readdata.php?roll_no="+ data;
pd.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
url,
new Response.Listener<String>() {
@SuppressLint("SetTextI18n")
@Override
public void onResponse(String response) {
pd.hide();
try {
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String id = jsonobject.getString("REGISTRATION_NO");
String stu = jsonobject.getString("STU_NAME").trim();
String fa = jsonobject.getString("FA_NAME").trim();
String mo = jsonobject.getString("MO_NAME").trim();
String eng = jsonobject.getString("ENG_TOTAL").trim();
String math = jsonobject.getString("MATH_TOTAL").trim();
String it = jsonobject.getString("IT_TOTAL").trim();
String pmo = jsonobject.getString("PMO_TOTAL").trim();
String pspc = jsonobject.getString("PSPC_TOTAL").trim();
String itlab = jsonobject.getString("IT_LAB_TOTAL").trim();
String pspclab = jsonobject.getString("PSPC_LAB_TOTAL").trim();
String total = jsonobject.getString("TOTAL_MARKS").trim();
String sgpa = jsonobject.getString("SGPA").trim();
String cgpa = jsonobject.getString("CGPA").trim();
result.setText("\t\t\t B.C.A 1ST SEM 2016\n \n Registration no. -" + id+"\n Student name -" + stu +"\n Father name -"+fa+"\n Mother Name -"+mo+"\n English Total -"+eng
+"\n Math Total -"+math+"\n It Total -"+it+"\n Pmo Total -"+pmo+"\n Pspc Total -"+pspc+"\n It lab -"+itlab+"\n Pspc lab -"+pspclab
+"\n Total Marks -"+total+"\n Sgpa -"+sgpa+"\n Cgpa -"+cgpa);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error != null) {
Toast.makeText(getApplicationContext(), "Something went wrong.", Toast.LENGTH_LONG).show();
}
}
}
);
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
}
}'
最佳答案
您可以使用 Volley 来做到这一点。我可以解释一下,但它有点长。基本上你必须在 PHP 中创建一个连接器,将它上传到你的服务器,然后使用 Volley 库将 android 应用程序连接到它。
但是这里有一个教程。这应该让你开始:
https://www.simplifiedcoding.net/retrieve-data-mysql-database-android/
关于php - 如何通过 TextView 中表的主键从android中的PHP服务器检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51862107/