【问题标题】:JSONException: Value .<!DOCTYPE of type java.lang.String cannot be converted to JSONObject errorJSONException: Value .<!DOCTYPE of type java.lang.String 无法转换为 JSONObject 错误
【发布时间】:2016-11-24 17:01:00
【问题描述】:

我已经在网上寻找我可能做错的解决方案,但我无法找到我的错误。 php 工作正常,所以我不确定我做错了什么。请帮忙!这是我的 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);

           }

当我在我的网络浏览器上键入地址时,php 工作正常,所以我确信问题出在活动上,但我不知道在哪里。这是我的主要活动。

      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();
}

还有我的日志猫

       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)

【问题讨论】:

  • 网络服务的输出是什么?它是否返回任何错误?
  • 内容类型/html输出是什么?
  • 在我看来,您忘记将响应的标头设置为 JSON,并且您收到的是 HTML 响应,而您的 Android 应用程序又无法将其解析为 JSON。在尝试解析之前在 Android Studio 中记录响应以查看您得到的结果 (Log.i("tag", response.toString());
  • 您收到的回复不正确。所以该响应将是 html 表单。

标签: php android json parsing android-studio


【解决方案1】:

将此添加到您的 php 脚本中并试一试

<?php

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

?>

【讨论】:

  • 嘿,侯萨姆。它很有效,我得到了大致的想法并且能够解决问题!谢谢☺
猜你喜欢
  • 2016-12-25
  • 2016-05-27
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多