gpt4 book ai didi

php - JSONException : Value . <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 错误

转载 作者:行者123 更新时间:2023-11-29 01:17:52 29 4
gpt4 key购买 nike

我已经在网上寻找我可能做错的解决方案,但我无法找到我的错误。 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 工作正常,所以我确信问题出在 Activity 上,但我不知道在哪里。这是我的主要 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();
}

还有我的日志

       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)

最佳答案

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

<?php

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

?>

关于php - JSONException : Value . &lt;!DOCTYPE of type java.lang.String cannot be converted to JSONObject 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38498520/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com