[英]How to fetch data from DB send response back in PHP?
Need to take the subject
provided by the user via android app and then use it to search database to find a teacher
name
and email
for that subject
and then send their email
, name
to android list view. 需要通过Android应用程序获取用户提供的
subject
,然后使用它来搜索数据库以查找该subject
的teacher
name
和email
,然后将其email
, name
发送到android列表视图。
PHP Code: PHP代码:
if ( !empty($_GET['subject']) ) {
$SUBJECT=$_GET['subject'];
$sql="SELECT IFF(sub== Name,Email,subjects_known FROM teacher_registration";
$res=$con->query($sql);
$result["Email"] = array();
$result["Name"] = array();
$result["Sub"] = array();
$row=array();
if($res->num_rows>0){
while($row=$res->fetch_assoc())
{
//$Name= array();
$Name= $row['Name'];
//array_push($result["Name"], $Name);
// $Email=array();
$Email= $row['Email'];
$Sub=$row['subjects_known'];
array_push($result["Email"],$Email);
array_push($result["Name"], $Name);
array_push($result["Sub"],$Sub);
// $result[] = $row;
}
// $r1=array();
// $r1['TEACHER']=$result;
echo json_encode($result,JSON_FORCE_OBJECT);
mysqli_close($con);
}
else
{
echo "Give the info";
mysqli_close($con);
}
adapter 适配器
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomListView extends BaseAdapter {
public String[] profilename;
public String[] Email;
LayoutInflater li;
public CustomListView(String[] p,String[] e,Teacher_searching ref) {
Context context = ref;
li=LayoutInflater.from(context);
this.profilename=p;
this.Email=e;
}
@Override
public int getCount() {
return Email.length;
}
@Override
public Object getItem(int position) {
return (position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
ViewHolder v=null;
if(convertView==null){
convertView = li.inflate(R.layout.layout, null);
v=new ViewHolder(convertView);
convertView.setTag(v);
}
else {
v=(ViewHolder)convertView.getTag();
}
v.tvw1.setText(profilename[position]);
v.tvw2.setText(Email[position]);
return convertView;
}
class ViewHolder{
TextView tvw1;
TextView tvw2;
ViewHolder(View v){
tvw1=v.findViewById(R.id.tvprofilename);
tvw2=v.findViewById(R.id.tvemail);
}
}
}
search initiator: 搜索发起者:
public class Teacher_searching extends AppCompatActivity {
String urladdress="https://light-headed-nests.000webhostapp.com/proed/searching1.php";
String[] name;
String[] Email;
String SUBJECT_SEARCH;
ListView listView;
BufferedInputStream is;
JsonObjectRequest jsonObjectRequest;
String subject_Search;
String line;
String result=null;
Bundle b=new Bundle();
JSONObject jsonObj = new JSONObject();
RequestQueue mqueue;
StringRequest s;
Integer i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_searching);
listView=(ListView) findViewById(R.id.lview);
SUBJECT_SEARCH=b.getString("Teacher");
// collectData();
mqueue= Volley.newRequestQueue(getApplicationContext());
jsonParse();
// onlyvolley();
Toast.makeText(getApplicationContext(),Email.length,Toast.LENGTH_SHORT).show();
/*CustomListView customListView=new CustomListView(name,Email,Teacher_searching.this);
listView.setAdapter(customListView);*/
}
/* private void onlyvolley() {
s=new StringRequest(Request.Method.POST, urladdress,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
showMsg("Server Response","Page not reachable");
}
)
}*/
private void jsonParse() {
jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, urladdress, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray=response.getJSONArray("TEACHER");
jsonObj=null;
name=new String[jsonArray.length()];
Email=new String[jsonArray.length()];
for (int i=0;i<=jsonArray.length();i++)
{
jsonObj=jsonArray.getJSONObject(i);
name[i]=jsonObj.getString("Name");
Email[i]=jsonObj.getString("Email");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<String,String>();
params.put("subject",SUBJECT_SEARCH);
return params;
}
};
mqueue.add(jsonObjectRequest);
}
/*private void collectData() {
try{
URL url=new URL(urladdress);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
subject_Search=b.getString("Teacher");
jsonObj.put("subject",subject_Search);
is=new BufferedInputStream(con.getInputStream());
}
catch (Exception ex) {}
//content
try{
InputStreamReader read=new InputStreamReader(is);
BufferedReader br=new BufferedReader(read);
StringBuilder sb=new StringBuilder();
while ((line=br.readLine())!=null){
sb.append(line).append("\n");
}
is.close();
result=sb.toString();
}
catch (Exception ex)
{
ex.printStackTrace();
}
//JSON
try{
JSONArray ja=new JSONArray("TEACHER");
JSONObject jo;
name=new String[ja.length()];
Email=new String[ja.length()];
ArrayList<HashMap<String, String>> mylist =new ArrayList<HashMap<String, String>>();
for(i=0;i<=ja.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
jo=ja.getJSONObject(i);
name[i]=jo.getString("Name");
Email[i]=jo.getString("Email");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}*/
}
searchin for:: 搜索::
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Search_teacher extends AppCompatActivity {
Spinner sub;
Button btn;
String sub_search[]={"Physics","Chemistry","Mathematics","Biology","English","Computer(C++)","Computer(Java)","History&Civics","Geography","Economics"};
String Sub_stud;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_teacher);
sub=findViewById(R.id.subject_search);
btn=findViewById(R.id.btn);
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,sub_search);
sub.setAdapter(adapter2);
sub.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0)
Sub_stud="Physics";
else if(i==1)
Sub_stud="Chemistry";
else if(i==2)
Sub_stud="Mathematics";
else if(i==3)
Sub_stud="Biology";
else if(i==4)
Sub_stud="English";
else if(i==5)
Sub_stud="Computer(C++)";
else if(i==6)
Sub_stud="Computer(Java)";
else if(i==7)
Sub_stud="History&Civics";
else if(i==8)
Sub_stud="Geography";
else if(i==9)
Sub_stud="Economics";
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void subject_teacher_search(View view) {
Intent i=new Intent(getApplicationContext(),Teacher_searching.class);
Bundle bundle=new Bundle();
bundle.putString("Teacher",Sub_stud);
i.putExtras(bundle);
startActivity(i);
}
}
stuff in list view: 列表视图中的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvprofilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="16dp"
android:textColor="#333"
android:textSize="22dp"
/>
<TextView
android:id="@+id/tvemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="50dp"
android:textColor="#333"
android:textSize="22dp"
/>
</LinearLayout>
the listview 列表视图
<?xml version="1.
0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Teacher_searching"
android:orientation="vertical">
<ListView
android:id="@+id/lview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Seems like there is a " sql syntax error " in your php code, anyway first resolve it appropriately and start doing the following steps in the application 似乎您的php代码中存在“ sql语法错误 ”,无论如何先适当地解决它,然后开始在应用程序中执行以下步骤
Step-1: Create a request queue using " Volley Library " to retrieve json response from server 步骤1:使用“ Volley Library ”创建请求队列以从服务器检索json响应
Step-2: Add a "Custom_Listview" in your layout file 步骤2:在布局文件中添加“ Custom_Listview”
Step-3: Finally set the " Adapter Class " to the listview.. That's it 步骤3:最后将“ Adapter Class ”设置为列表视图。就这样
Happy Coding!! 快乐编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.