首页 > 解决方案 > W/System.err: org.json.JSONException: 在字符 1 处输入结束 {

问题描述

我正在尝试获取有关用户当前位置的所有附近地点的名称列表(仅),但我收到了错误消息。当我按下“显示列表”按钮时。


MainActivity.java

package com.example.experimentmaps;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ArrayList<String> arrayList;
ListView listView;
ArrayAdapter arrayAdapter;

public void onClick(View v){
    try {
        DownloadTask task = new DownloadTask();
        task.execute("https://maps.googleapis.com/maps/api/place/nearbysearch/json? 
location=-33.8670522,151.1957362
&radius=1500
&type=restaurant
&keyword=cruise
&key=AIzaSy***********************_4-M");
    }catch (Exception e){
        e.printStackTrace();
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView=findViewById(R.id.listview);


    arrayList = new ArrayList<>();
    arrayAdapter = new 
 ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);

    listView.setAdapter(arrayAdapter);

}

public class DownloadTask extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String... urls) {
        String result ="";
        URL url;
        HttpURLConnection urlConnection = null;
        try{
            url = new URL(urls[0]);

            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = urlConnection.getInputStream();

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

            int data = inputStreamReader.read();
            while (data!=-1){
                char current = (char) data;
                result +=current;
                data = inputStream.read();
            }
            return result;

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

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        try {
            JSONObject jsonObject = new JSONObject(s);//as s corresponds to result

            String placesinformation = jsonObject.getString("results");

            JSONArray jsonArray = new JSONArray(placesinformation);

            String placesname="";
            int numberofitems = 20;
            if(jsonArray.length()<5){
                numberofitems = jsonArray.length();
            }

            for(int i=0;i<numberofitems;i++){
                JSONObject jsonpart = jsonArray.getJSONObject(i);
                String name = jsonpart.getString("name");

                if(!name.equals("")){
                    placesname += name;
                }

                if(!placesname.equals("")){
                    arrayList.add(placesname);
                }
            }
            arrayAdapter.notifyDataSetChanged();

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

这是我应该根据文档通过上述网址获得的 JSON 响应

 {
"html_attributions" : [],
"results" : [
  {
     "geometry" : {
        "location" : {
           "lat" : -33.870775,
           "lng" : 151.199025
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent- 
 71.png",
     "id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
     "name" : "Rhythmboat Cruises",
     "opening_hours" : {
        "open_now" : true
     },
     "photos" : [
        {
           "height" : 270,
           "html_attributions" : [],
           "photo_reference" : "CnRnAAAAF- 
 LjFR1ZV93eawe1cU_3QNMCNmaGkowY7CnOf- 
 kcNmPhNnPEG9W979jOuJJ1sGr75rhD5hqKzjD8vbMbSsRnq_
 Ni3ZIGfY6hKWmsOf3qHKJInkm4h55lzvL 
 AXJVc-Rr4kI9O1tmIblblUpg2oqoq8RIQRMQJ
 hFsTr5s9haxQ07EQHxoUO0ICubVFGYfJiMUPor1GnIWb5i8",
            "width" : 519
        }
     ],
     "place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
     "reference" : "CoQBdQAAAFSiijw5-cAV68xdf2O18pKIZ0seJh03u9h9wk_lEdG- 
cP1dWvp_QGS4SNCBMk_fB06YRsfMrNkINtPez22p5lRIlj5ty_HmcNw
cl6GZXbD2RdXsVfLYlQwnZQcnu 
7ihkjZp_2gk1-fWXql3GQ8-1BEGwgCxG-eaSnIJIBPuIpihEhAY1WYdxPvOWsPnb2- 
nGb6QGhTipN0lgaLpQTnkcMeAIEvCsSa0Ww",
     "types" : [ "travel_agency", "restaurant", "food", "establishment" ],
     "vicinity" : "Pyrmont Bay Wharf Darling Dr, Sydney"
  },
  {
     "geometry" : {
        "location" : {
           "lat" : -33.866891,
           "lng" : 151.200814
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant- 
71.png",
     "id" : "45a27fd8d56c56dc62afc9b49e1d850440d5c403",
     "name" : "Private Charter Sydney Habour Cruise",
     "photos" : [
        {
           "height" : 426,
           "html_attributions" : [],
           "photo_reference" : 
"CnRnAAAAL3n0Zu3U6fseyPl8URGKD49aGB2Wka7CKDZfamoGX2ZTLMBYgTUshjr- 
 MXc0_O2BbvlUAZWtQTBHUVZ-5Sxb1-P-VX2Fx0sZF87q- 
 9vUt19VDwQQmAX_mjQe7UWmU5lJGCOXSgxp2fu1b5VR_PF
 31RIQTKZLfqm8TA1eynnN4M1XShoU8adzJC 
 cOWK0er14h8SqOIDZctvU",
           "width" : 640
        }
     ],
     "place_id" : "ChIJqwS6fjiuEmsRJAMiOY9MSms",
     "reference" : 
"CpQBhgAAAFN27qR_t5oSDKPUzjQIeQa3lrRpFTm5alW3ZYbMFm8k10ETbISfK9S1nwcJVfrP- 
bjra7NSPuhaRulxoonSPQklDyB-xGvcJncq6qDXIUQ3hlI- 
bx4AxYckAOX74LkupHq7bcaREgrSBE- 
U6GbA1C3U7I- 
HnweO4IPtztSEcgW09y03v1hgHzL8xSDElmkQtRIQzLbyBfj3e0FhJzABXjM2QBoUE2EnL- 
DzWrzpgmMEulUBLGrtu2Y",
     "types" : [ "restaurant", "food", "establishment" ],
     "vicinity" : "Australia"
  },
  {
     "geometry" : {
        "location" : {
           "lat" : -33.870943,
           "lng" : 151.190311
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant- 
 71.png",
     "id" : "30bee58f819b6c47bd24151802f25ecf11df8943",
     "name" : "Bucks Party Cruise",
     "opening_hours" : {
        "open_now" : true
     },
     "photos" : [
        {
           "height" : 600,
           "html_attributions" : [],
           "photo_reference" : 
"CnRnAAAA48AX5MsHIMiuipON_Lgh97hPiYDFkxx_vnaZQMO
 cvcQwYN92o33t5RwjRpOue5R47AjfMltn 
 toz71hto40zqo7vFyxhDuuqhAChKGRQ5mdO5jv5
 CKWlzi182PICiOb37PiBtiFt7lSLe1SedoyrD- 
 xIQD8xqSOaejWejYHCN4Ye2XBoUT3q2IXJQpMkmffJiBNftv8QSwF4",
           "width" : 800
        }
     ],
     "place_id" : "ChIJLfySpTOuEmsRsc_JfJtljdc",
     "reference" : "CoQBdQAAANQSThnTekt- 

UokiTiX3oUFT6YDfdQJIG0ljlQnkLfWefcKmjxax0xmUpWjmpWdOsSc
l9zSyBNImmrTO9AE9DnWTdQ2hY 
7n-OOU4UgCfX7U0TE1Vf7jyODRISbK- 
u86TBJij0b2i7oUWq2bGr0cQSj8CV97U5q8SJR3AFDYi3ogqE
hCMXjNLR1k8fiXTkG2BxGJmGhTqwE8C4 
grdjvJ0w5UsAVoOH7v8HQ",
     "types" : [ "restaurant", "food", "establishment" ],
     "vicinity" : "37 Bank St, Pyrmont"
  },
  {
     "geometry" : {
        "location" : {
           "lat" : -33.867591,
           "lng" : 151.201196
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/
 place_api/icons/travel_agent- 71.png",
     "id" : "a97f9fb468bcd26b68a23072a55af82d4b325e0d",
     "name" : "Australian Cruise Group",
     "opening_hours" : {
        "open_now" : true
     },
     "photos" : [
        {
           "height" : 242,
           "html_attributions" : [],
           "photo_reference" : 


 "CnRnAAAABjeoPQ7NUU3pDitV4Vs0BgP1FLhf_iCgStUZUr4Z
 uNqQnc5k43jbvjKC2hTGM8SrmdJYyOyx 


RO3D2yutoJwVC4Vp_dzckkjG35L6LfMm5sjrOr6uyOtr2PNC
p1xQylx6vhdcpW8yZjBZCvVsjNajLBIQ- 
z4ttAMIc8EjEZV7LsoFgRoU6OrqxvKCnkJGb9F16W57iIV4LuM",
           "width" : 200
        }
     ],
     "place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
     "reference" : "CoQBeQAAAFvf12y8veSQMdIMmAXQmus1zqkgKQ- 
O2KEX0Kr47rIRTy6HNsyosVl0CjvEBulIu_cujrSOgICdcxNioFDHtAxXBhqeR- 

8xXtm52Bp0lVwnO3LzLFY3jeo8WrsyIwNE1kQlG
uWA4xklpOknHJuRXSQJVheRlYijOHSgsBQ35mOcEhC 
5IpbpqCMe82yR136087wZGhSziPEbooYkHLn9e5njOTuBprcfVw",
     "types" : [ "travel_agency", "restaurant", "food", "establishment" ],
     "vicinity" : "32 The Promenade, King Street Wharf 5, Sydney"
  }
],
"status" : "OK"
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/processurl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="48dp"
    android:layout_marginLeft="48dp"
    android:layout_marginTop="92dp"
    android:onClick="onClick"
    android:text="Show List"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView
    android:id="@+id/listview"
    android:layout_width="312dp"
    android:layout_height="466dp"
    android:layout_marginStart="1dp"
    android:layout_marginLeft="1dp"
    android:layout_marginTop="1dp"
    android:layout_marginEnd="1dp"
    android:layout_marginRight="1dp"
    android:layout_marginBottom="1dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.494"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/processurl"
    app:layout_constraintVertical_bias="0.26" />


</androidx.constraintlayout.widget.ConstraintLayout>

在清单中使用权限---->

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

build.gradle 中的依赖:---->

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.google.android.libraries.places:places:1.1.0'

}

日志猫(错误):--->

2020-08-21 13:55:39.111 31636-31636/com.example.experimentmaps W/System.err: org.json.JSONException: 
End of input at character 1 of {
2020-08-21 13:55:39.112 31636-31636/com.example.experimentmaps W/System.err:     at 
org.json.JSONTokener.syntaxError(JSONTokener.java:460)
2020-08-21 13:55:39.112 31636-31636/com.example.experimentmaps W/System.err:     at 
org.json.JSONTokener.nextValue(JSONTokener.java:101)
2020-08-21 13:55:39.112 31636-31636/com.example.experimentmaps W/System.err:     at 
org.json.JSONTokener.readObject(JSONTokener.java:371)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
org.json.JSONTokener.nextValue(JSONTokener.java:104)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
org.json.JSONObject.<init>(JSONObject.java:165)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
org.json.JSONObject.<init>(JSONObject.java:182)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
com.example.experimentmaps.MainActivity$DownloadTask.onPostExecute(MainActivity.java:89)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
com.example.experimentmaps.MainActivity$DownloadTask.onPostExecute(MainActivity.java:54)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
android.os.AsyncTask.finish(AsyncTask.java:771)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
android.os.AsyncTask.access$900(AsyncTask.java:199)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
2020-08-21 13:55:39.113 31636-31636/com.example.experimentmaps W/System.err:     at 
android.os.Handler.dispatchMessage(Handler.java:106)
2020-08-21 13:55:39.114 31636-31636/com.example.experimentmaps W/System.err:     at 
android.os.Looper.loop(Looper.java:223)
2020-08-21 13:55:39.116 31636-31636/com.example.experimentmaps W/System.err:     at 
android.app.ActivityThread.main(ActivityThread.java:7476)
2020-08-21 13:55:39.117 31636-31636/com.example.experimentmaps W/System.err:     at 
java.lang.reflect.Method.invoke(Native Method)
2020-08-21 13:55:39.117 31636-31636/com.example.experimentmaps W/System.err:     at 
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
2020-08-21 13:55:39.117 31636-31636/com.example.experimentmaps W/System.err:     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:939)

有了 JSON 的一些基本知识,我试图在一个列表中显示所有附近的地方,这个项目可能包含其他错误(也许),任何建议(我的意思是任何错误写或遗漏的内容)将不胜感激!

提前感谢您的帮助!

请有人帮忙,我10天前发布了这个问题仍然没有得到任何答案..

标签: androidarraysjson

解决方案


首先,要发现问题,您需要添加 log.d 语句以在每个阶段之后将接收到的 JSON 字符串显示到 Logcat,就像在接收到字符串后一样,例如:在 doInBackground() 中返回之前,然后运行您的应用程序并观察在 Logcat 中收到结果。其次,您不能像对 URL 字符串所做的那样在多行中编写字符串,您需要将其写在一行中,或者在向字符串添加新行时至少使用字符串连接运算符“+”。URL 中也不能有空格,因为它必须不包含空格才能工作。第三,您正在使用字符串来存储结果流的每个字符,这是低效的,因为 String 在 Java 中是不可变的,因此您正在为流的每个字符创建一个新的 String 对象,因此最好使用 StringBuilder像这样有效地做到这一点:

protected String doInBackground(String... urls) {
            try {
                URL url = new URL(urls[0]);
                HttpsURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection();
                int responseCode = ((HttpURLConnection) url.openConnection())
                        .getResponseCode();
                Log.i("TAGf", "doInBackground: responseCode:" + responseCode);
                httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.76");
                httpURLConnection.connect();

                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder total = new StringBuilder();
                for (String line; (line = r.readLine()) != null; ) {

                    total.append(line).append('\n');
                   
                }

                Log.i("RESULT", "result: "+total.toString());
                return total.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return "Error in PhoneDownloader Async Task";
            }
        }

推荐阅读