问题
I am attempting to send a string json of teams through Bluetooth to another android device. For some reason the Serializer will load a small list of teams but a large list of teams causes this error:
01-21 13:21:30.584 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Unterminated string at character 990 of [{"teleStackDirection":0,"teleCoopSet":0,"autoTotes":0,"teleCoopStack":0,"autoProgs":0,"telePlatform":0,"teleStackTotes":0,"mechLitterPusher":false,"mechLitterInserter":false,"teamNum":0,"mechToteFeeder":false,"teleCarryTote":0,"autoPos":0,"autoMoveTote":false,"mechContainerFlipper":false,"teleHumanStation":0,"id":"6d1d1c80-d386-401c-91c7-3260fdb28a9b","autoMove":false,"telePickUpLitter":false,"autoContainers":0,"teleMoveLitter":false,"teleStackContainers":0,"mechContainerStepRemover":false,"telePlaceTote":0,"telePlaceLitter":0},{"teleStackDirection":0,"teleCoopSet":0,"autoTotes":0,"teleCoopStack":0,"autoProgs":0,"telePlatform":0,"teleStackTotes":0,"mechLitterPusher":false,"mechLitterInserter":false,"teamNum":1,"mechToteFeeder":false,"teleCarryTote":0,"autoPos":0,"autoMoveTote":false,"mechContainerFlipper":false,"teleHumanStation":0,"id":"e10f158f-7254-4bfb-85eb-eb402be790c3","autoMove":false,"telePickUpLitter":false,"autoContainers":0,"teleMoveLitter":false,"teleStackContain
01-21 13:21:30.618 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value toPos" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.648 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value kContainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.656 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value lse of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.662 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value e of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.728 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value gs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.734 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value gs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.740 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value {"teleCoopSet":0,"teleStackDirection":0,"autoTotes":0,"autoProgs":0,"teleCoopStack":0,"mechLitterPusher":false,"teleStackTotes":0,"telePlatform":0,"mechLitterInserter":false,"teamNum":34,"mechToteFeeder":false,"autoMoveTote":false,"autoPos":0,"teleCarryTote":0,"mechContainerFlipper":false,"teleHumanStation":0,"id":"c2108315-2ea6-4cd5-8205-c845ae48739c","autoMove":false,"autoContainers":0,"telePickUpLitter":false,"teleStackContainers":0,"teleMoveLitter":false,"mechContainerStepRemover":false,"telePlaceLitter":0,"telePlaceTote":0} of type org.json.JSONObject cannot be converted to JSONArray
01-21 13:21:30.757 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value {"teleCoopSet":0,"teleStackDirection":0,"autoTotes":0,"autoProgs":0,"teleCoopStack":0,"mechLitterPusher":false,"teleStackTotes":0,"telePlatform":0,"mechLitterInserter":false,"teamNum":34,"mechToteFeeder":false,"autoMoveTote":false,"autoPos":0,"teleCarryTote":0,"mechContainerFlipper":false,"teleHumanStation":0,"id":"c2108315-2ea6-4cd5-8205-c845ae48739c","autoMove":false,"autoContainers":0,"telePickUpLitter":false,"teleStackContainers":0,"teleMoveLitter":false,"mechContainerStepRemover":false,"telePlaceLitter":0,"telePlaceTote":0} of type org.json.JSONObject cannot be converted to JSONArray
01-21 13:21:30.775 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value {"teleCoopSet":0,"teleStackDirection":0,"autoTotes":0,"autoProgs":0,"teleCoopStack":0,"mechLitterPusher":false,"teleStackTotes":0,"telePlatform":0,"mechLitterInserter":false,"teamNum":34,"mechToteFeeder":false,"autoMoveTote":false,"autoPos":0,"teleCarryTote":0,"mechContainerFlipper":false,"teleHumanStation":0,"id":"c2108315-2ea6-4cd5-8205-c845ae48739c","autoMove":false,"autoContainers":0,"telePickUpLitter":false,"teleStackContainers":0,"teleMoveLitter":false,"mechContainerStepRemover":false,"telePlaceLitter":0,"telePlaceTote":0} of type org.json.JSONObject cannot be converted to JSONArray
01-21 13:21:30.777 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value ners" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.781 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value lePickUpLitter" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.787 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value HumanStation" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.794 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value CarryTote" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.801 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value sher" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.807 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value teleCoopStack" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.866 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value chToteFeeder" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.873 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value ackTotes" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.879 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopSet" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.886 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value ver" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.894 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.900 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.904 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.905 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.908 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.909 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.911 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.912 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.914 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.915 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.916 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value tainers" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.958 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value eStackDirection" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.967 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value kUpLitter" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.974 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Tote" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.981 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.988 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.992 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.994 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:30.995 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value oopStack" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.042 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.048 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.053 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.055 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.056 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.058 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.059 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.063 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.065 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value Flipper" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.123 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.127 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.133 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.135 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.137 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.139 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
01-21 13:21:31.141 9596-9596/org.huntingtonrobotics.frcrecyclerushpitscouter D/FRCRecycleRushPitScouterJSONSerializer﹕ Error: org.json.JSONException: Value rs" of type java.lang.String cannot be converted to JSONArray
01-21 13:26:07.836 12185-12185/org.huntingtonrobotics.frcrecyclerushpitscouter I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM build: (N/A)
OpenGL ES Shader Compiler Version: E031.24.00.00
Build Date: 11/12/13 Tue
Local Branch:
Remote Branch:
Local Patches:
Here is where I send and read the string:
case Constants.MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = mConversationJSONString;
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
//Log.i(TAG, readMessage);
try {
mSerilizer.saveBluetoothTeams(readMessage);
}catch (JSONException je){
Log.d(TAG, "Error: " + je);
}catch (Exception e){
Log.d(TAG, "Error: " + e);
}
mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
break;
And here is part of my Serializer class where I save the teams on the device where the file was sent:
//returns string version of json file
public String getJSONString() throws JSONException, IOException{
//Build an array in JSON
JSONArray array = new JSONArray();
loadTeams();
for (Team c : mTeams) {
array.put(c.toJSON());
}
return array.toString();
}
//load teams from file system
public Boolean saveBluetoothTeams(String s) throws IOException, JSONException {
BufferedReader reader = null;
try {
//parse the JSON using JSONTokener
JSONArray jsonArray = new JSONArray(s);
ArrayList<Team> bluetoothListData = new ArrayList<Team>();
//build the array of teams from JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
bluetoothListData.add(new Team(jsonArray.getJSONObject(i)));
}
mTeams.addAll(bluetoothListData);
saveTeams(mTeams);
return true;
}catch (FileNotFoundException e) {
//ignore
return false;
}catch (Exception e){
Log.d(TAG, "Error: " + e);
return false;
}finally {
//ensures the underlying file handle is freed up
if (reader != null)
reader.close();
}
}
Full Bluetooth Chat Service Class: https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java
Full Bluetooth Chat Fragment class: https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java
回答1:
The problem appears to be that your data is getting chunked, and you are trying to parse those chunks individually.
There are number of ways to approach this problem. One easy way would be to use ObjectInputStream and ObjectOutputStream instead of plain InputStream
and OutputStream
. The Object stream variants will handle ensuring that you are sending and receiving complete objects, so if you send a String in an output stream, you should get the complete stream back on the other side in the input stream.
Assuming you are sending the JSON as a String (and it is UTF-8 encoded), sending the JSON is as easy as calling writeUTF(String)
, then reading it using readUTF()
.
回答2:
@Tanis.7x was correct that I was receiving my data in chunks. However I could not get the Object Input/Output Stream to work so I used this method:
create a few random characters to put at the end of the data you are sending over Bluetooth.
//to put on end of string being sent over bluetooth private static final String SUPER_SECRET_PASSWORD = "~~~";
create a string to be the final string that holds all the chunks
//for building final string from bluetooth receiver since JSONString was being received in chunks private String mFinalString = "";
build string from Bluetooth data but don't process it till it has the random characters at the end of it.
if ((mFinalString.substring(mFinalString.length()-SUPER_SECRET_PASSWORD.length()).equals(SUPER_SECRET_PASSWORD))){ try { //remove the super secret password from mFinalString mFinalString = mFinalString.substring(0, mFinalString.length()- SUPER_SECRET_PASSWORD.length()); //print mFinalString out in the TextView mConversationArrayAdapter.add(mConnectedDeviceName + ": " + mFinalString); //process mFinalString in the Serializer mSerializer.saveBluetoothTeams(mFinalString); //clear mFinalString mFinalString = ""; Toast.makeText(activity, "Teams Received. Please close your app and restart to reload teams.", Toast.LENGTH_LONG).show(); }catch (Exception e){ Log.e(TAG, "ERROR Saving mFinalString: " + e); } }
All of my code for my Bluetooth Fragment Class can be found here: https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java
来源:https://stackoverflow.com/questions/28074603/jsonstring-wont-convert-to-jsonarray