JSONString 不会转换为 JSONArray
JSONString won't convert to JSONArray
我正在尝试通过蓝牙将一串 json 个团队发送到另一个 android 设备。出于某种原因,Serializer 将加载一小部分团队,但大量团队会导致此错误:
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:
这里是我发送和读取字符串的地方:
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;
这是我的序列化程序 class 的一部分,我将团队保存在发送文件的设备上:
//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();
}
}
问题似乎是您的数据正在分块,而您正在尝试单独解析这些块。
有多种方法可以解决这个问题。一种简单的方法是使用 ObjectInputStream
and ObjectOutputStream
而不是普通的 InputStream
和 OutputStream
。对象流变体将确保您发送和接收完整的对象,因此如果您在输出流中发送一个字符串,您应该在输入流的另一端返回完整的流。
假设您将 JSON 作为字符串发送(并且它是 UTF-8 编码的),发送 JSON 就像调用 writeUTF(String)
一样简单,然后使用readUTF()
.
@Tanis.7x 是正确的,我正在分块接收数据。但是我无法让 Object Input/Output Stream 工作所以我使用了这个方法:
创建一些随机字符放在您通过蓝牙发送的数据的末尾。
//to put on end of string being sent over bluetooth
private static final String SUPER_SECRET_PASSWORD = "~~~";
创建一个字符串作为包含所有块的最终字符串
//for building final string from bluetooth receiver since JSONString was being received in chunks
private String mFinalString = "";
从蓝牙数据构建字符串,但在其末尾具有随机字符之前不要处理它。
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);
}
}
我的蓝牙片段 Class 的所有代码都可以在这里找到:
https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java
我正在尝试通过蓝牙将一串 json 个团队发送到另一个 android 设备。出于某种原因,Serializer 将加载一小部分团队,但大量团队会导致此错误:
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:
这里是我发送和读取字符串的地方:
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;
这是我的序列化程序 class 的一部分,我将团队保存在发送文件的设备上:
//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();
}
}
问题似乎是您的数据正在分块,而您正在尝试单独解析这些块。
有多种方法可以解决这个问题。一种简单的方法是使用 ObjectInputStream
and ObjectOutputStream
而不是普通的 InputStream
和 OutputStream
。对象流变体将确保您发送和接收完整的对象,因此如果您在输出流中发送一个字符串,您应该在输入流的另一端返回完整的流。
假设您将 JSON 作为字符串发送(并且它是 UTF-8 编码的),发送 JSON 就像调用 writeUTF(String)
一样简单,然后使用readUTF()
.
@Tanis.7x 是正确的,我正在分块接收数据。但是我无法让 Object Input/Output Stream 工作所以我使用了这个方法:
创建一些随机字符放在您通过蓝牙发送的数据的末尾。
//to put on end of string being sent over bluetooth private static final String SUPER_SECRET_PASSWORD = "~~~";
创建一个字符串作为包含所有块的最终字符串
//for building final string from bluetooth receiver since JSONString was being received in chunks private String mFinalString = "";
从蓝牙数据构建字符串,但在其末尾具有随机字符之前不要处理它。
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); } }
我的蓝牙片段 Class 的所有代码都可以在这里找到: https://github.com/FRCTeam1501/FRCRecycleRushPitScouter/blob/master/app/src/main/java/org/huntingtonrobotics/frcrecyclerushpitscouter/bluetoothchat/BluetoothChatFragment.java