JSONException:Value。<!类型java.lang.String的DOCTYPE无法转换为JSONObject错误

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+"
Speciality:\t" +Speciality+ "
Hospital:\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)
dou91808
dou91808 你得到的回答是不正确的。所以响应将是html形式。
大约 4 年之前 回复
dongna9185
dongna9185 stackoverflow.com/questions/10267910/...看到这个..
大约 4 年之前 回复
drd0833
drd0833 在我看来,您忘记将响应的标头设置为JSON,并且您正在获取HTML响应,而您的Android应用无法将其解析为JSON。在尝试解析它之前在Android工作室中记录响应以查看您正在获取的内容(Log.i(“tag”,response.toString());
大约 4 年之前 回复
douli7841
douli7841 什么是内容类型/html输出?
大约 4 年之前 回复
doukucai0099
doukucai0099 Web服务的输出是什么?它是否返回任何错误?
大约 4 年之前 回复

1个回答

Add this to your php script and give it a try

<?php

header('Content-type: application/json');

?>
doufeng3602
doufeng3602 嘿侯萨姆。 它工作,我有一般的想法,但能够解决问题! Thanksâº
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐