duanhao5038 2016-07-21 08:09
浏览 227

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)
  • 写回答

1条回答 默认 最新

  • dotelauv682684 2016-07-21 08:17
    关注

    Add this to your php script and give it a try

    <?php
    
    header('Content-type: application/json');
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键