Reputation: 332
I have looked for solutions online on what I might be doing wrong but I am unable to find my mistake. The php works fine so am not sure what I am doing wrong. Please help! Here is my php.
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$Speciality = $_GET['Speciality'];
require_once('db_config.php');
$sql = "SELECT * FROM doctor WHERE Speciality='".$Speciality."'";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"Name"=>$res['Name'],
"Speciality"=>$res['Speciality'],
"Hospital"=>$res['Hospital']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}
The php works fine when I type the address on my web browser so am convinced the problem is on the activity but I dont know where. Here is my main Activity.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//For the appointments
public static final String DATA_URL = "http://XXXXX.com/doctor.php?Speciality=";
public static final String KEY_NAME = "Name";
public static final String KEY_SPECIALITY = "Speciality";
public static final String KEY_HOSPITAL = "Hospital";
public static final String JSON_ARRAY = "result";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewResult = (TextView) findViewById(R.id.textViewResult);
}
private void getData() {
Spinner spinner=(Spinner)findViewById(R.id.spinnerdoctor);
String doctor = spinner.getSelectedItem().toString();
textViewResult = (TextView) findViewById(R.id.textViewResult);
if (doctor.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = DATA_URL+doctor;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Could not connect. Please try again",Toast.LENGTH_LONG).show();
loading.cancel();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String Name="";
String Speciality="";
String Hospital = "";
textViewResult = (TextView) findViewById(R.id.textViewResult);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
Name = collegeData.getString(KEY_NAME);
Speciality = collegeData.getString(KEY_SPECIALITY);
Hospital = collegeData.getString(KEY_HOSPITAL);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+Name+"\nSpeciality:\t" +Speciality+ "\nHospital:\t"+ Hospital);
}
public void loaddocs(View v) {
getData();
}
And my logcat
07-21 10:54:30.629 4221-4221/com.kirathe.mos.afriemergency
W/System.err: org.json.JSONException: Value .<!DOCTYPE of type java.lang.String cannot be converted to JSONObject
07-21 10:54:30.631 4221-4221/com.kirathe.mos.afriemergency W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
07-21 10:54:30.631 4221-4221/com.kirathe.mos.afriemergency W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
07-21 10:54:30.631 4221-4221/com.kirathe.mos.afriemergency W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
07-21 10:54:30.631 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.kirathe.mos.afriemergency.MainActivity.showJSON(MainActivity.java:389)
07-21 10:54:30.631 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.kirathe.mos.afriemergency.MainActivity.access$100(MainActivity.java:45)
07-21 10:54:30.631 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.kirathe.mos.afriemergency.MainActivity$2.onResponse(MainActivity.java:368)
07-21 10:54:30.631 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.kirathe.mos.afriemergency.MainActivity$2.onResponse(MainActivity.java:364)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at android.os.Looper.loop(Looper.java:211)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5389)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
07-21 10:54:30.632 4221-4221/com.kirathe.mos.afriemergency W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Upvotes: 3
Views: 2304
Reputation: 45
Add this to your php script and give it a try
<?php
header('Content-type: application/json');
?>
Upvotes: 1