簡體   English   中英

[英]Value <br of type java.lang.String cannot be converted to JSONObject even though the response is in JSON format

Android代碼:

OkHttpClient client = new OkHttpClient();
Request request = new 
Request.Builder().url("http://10.0.2.2/TEST/Latest/getBatsman.php?var="+var)
.build();


Response response = client.newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
JSONArray jsonArray = jsonObject.getJSONArray("batsman");

PHP代碼:

include('DBdetails.php');
global $conn;
$id = $_GET["var"];

$sql = "SELECT * FROM `all_players` where teamname='".$id."'";
$result = $conn->query($sql) ;
$temparray =  array();

while($row = $result->fetch_assoc())
{
$temparray[] = $row;
}
$tOutput = json_encode($temparray);
echo json_encode(array('batsman' => json_decode($tOutput) ));

例外情況:

W/System.err: org.json.JSONException: Value <br of type java.lang.String 
cannot be converted to JSONObject
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)

JSON響應在瀏覽器中嘗試過,沒有html響應:

{  
   "batsman":[  
      {  
         "player_id":"16",
         "player_name":"Steve Smith",
         "player_cat":"2",
         "player_popularity":"95%",
         "player_image":"https:\/\/s29.postimg.org\/ch1qppoaf\/stevesmith.png",
         "teamname":"AUS"
      },
      {  
         "player_id":"17",
         "player_name":"aaron finch",
         "player_cat":"2",
         "player_popularity":"90%",
         "player_image":"https:\/\/s9.postimg.org\/486myc8bz\/aaronfinch.png ",
         "teamname":"AUS"
      },
      {  
         "player_id":"18",
         "player_name":"adam zampa",
         "player_cat":"0",
         "player_popularity":"85%",
         "player_image":"https:\/\/s22.postimg.org\/uoc40q9lt\/adamzampa.png",
         "teamname":"AUS"
      },
      {  
         "player_id":"19",
         "player_name":"chris lynn",
         "player_cat":"0",
         "player_popularity":"80%",
         "player_image":"https:\/\/s8.postimg.org\/sm6iv0645\/chrislynn.png",
         "teamname":"AUS"
      },
      {  
         "player_id":"20",
         "player_name":"david warner",
         "player_cat":"2",
         "player_popularity":"93%",
         "player_image":"https:\/\/s30.postimg.org\/8jza6ntq9\/davidwarner.png",
         "teamname":"AUS"
      },
      {  
         "player_id":"21",
         "player_name":"glenn maxwell",
         "player_cat":"0",
         "player_popularity":"92%",
         "player_image":"https:\/\/s13.postimg.org\/f04aokpon\/glennmaxwell.png",
         "teamname":"AUS"
      },
      {  
         "player_id":"22",
         "player_name":"james pattinson",
         "player_cat":"0",
         "player_popularity":"95%",
         "player_image":"https:\/\/s7.postimg.org\/gm43j4ptn\/jamespattinson.png",
         "teamname":"AUS"
      },
      {  
         "player_id":"23",
         "player_name":"johnha sings",
         "player_cat":"1",
         "player_popularity":"90%",
         "player_image":"https:\/\/s13.postimg.org\/xdjv4653b\/johnhasings.png",
         "teamname":"AUS"
      },
      {  
         "player_id":"24",
         "player_name":"joshhazlewood",
         "player_cat":"1",
         "player_popularity":"85%",
         "player_image":"https:\/\/s23.postimg.org\/f7w433o9n\/joshhazlewood.png ",
         "teamname":"AUS"
      },
      {  
         "player_id":"25",
         "player_name":"matthewwade",
         "player_cat":"4",
         "player_popularity":"85%",
         "player_image":"https:\/\/s3.postimg.org\/79lhqd80j\/matthewwade.png",
         "teamname":"AUS"
      }
   ]
}

希望這段代碼對您有所幫助。

String post(String url, JSONObject login) {
    try {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, login.toString());
        okhttp3.Request request = new okhttp3.Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Authorization", "Bearer " + token)
                .build();

        okhttp3.Response response = client.newCall(request).execute();

        try {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

String response;

private class AsyncTaskRunner extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(String... params) {

        try {
            response = post(
                    insertUrl, jsonObject);
            Log.d("PUT JSON ARRAY___", jsonObject.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.d("---OKHTTP---", response);
        try {
            JSONObject jobj = new JSONObject(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

價值

[英]Value <br of type java.lang.String cannot be converted to JSONObject - android php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM