2016-07-25 63 views
0

我是一名android初学者,请对此进行必要的帮助。获取异常:org.json.JSONObject无法转换为JSONArray

try { 
JSONArray jArray = new JSONArray(result); 
// Extract data from json and store into ArrayList as class objects 
for (int i = 0; i < jArray.length(); i++) { 
    JSONObject json_data = jArray.getJSONObject(i); 
    DataFish fishData = new DataFish(); 
    fishData.nonce = json_data.getString("nonce"); 
    fishData.iUserId = json_data.getString("iUserId"); 
    fishData.timestamp = json_data.getString("timestamp"); 
    fishData.token = json_data.getString("token"); 

    data.add(fishData); 
} 

// Setup and Handover data to recyclerview 
mRVFishPrice = (RecyclerView) findViewById(R.id.fishPriceList); 
mAdapter = new AdapterFish(PostActivity.this, data); 
mRVFishPrice.setAdapter(mAdapter); 
mRVFishPrice.setLayoutManager(new LinearLayoutManager(PostActivity.this)); 

} 
catch (JSONException e) { 
Toast.makeText(PostActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 
} 

我把它转换成JSONArray还是我越来越

org.json.JSONException:值{ “代码”: “0”, “消息”:“访问被拒绝, 无效用户参考“}类型org.json.JSONObject不能被 转换为JSONArray。

+0

发布您的JSON数据。 –

+0

首先检查你的代码是o还是1.如果代码1得到数组值 – vinoth12594

+0

使用代码键整数 – vinoth12594

回答

0

TRY it。

JSONArray alldata = new JSONArray(); 

for(your codition to get data for how many array items in loop){ 
JSONObject all = new JSONObject(); 
all.put("nonce", "G9Ivek"); 
all.put("iUserId", "477"); 
all.put("timestamp","20160614060439"); 
alldata.put(all); 

}

和发送ALLDATA服务器。

希望这将有助于you..Let我知道..

对于异步任务DOINBACKGROUND发送到服务器

JSONObject objMainList = new JSONObject(); 
objMainList.put("ALLDETAILS", alldata); 

对于绑定日期API URL()

Map<String, String> param = new HashMap<String, String>(); 
        param.put("access_code", access_code); 
        param.put("auth_token", auth_token); 
        param.put("data", objMainList.toString()); 

        try { 
         HttpUtility.sendPostRequest(Config.Submit_Answer, param); 
         String response = HttpUtility.readMultipleLinesRespone(); 
         json = new JSONObject(response); 
         HttpUtility.disconnect(); 

} catch (Exception e) { 

         e.printStackTrace(); 

        } 
+0

写什么来发送alldata到服务器? – Rujuta

+0

请按照上面的答案发送到服务器..并让我知道。 – Andolasoft

0

这不是一个JSONArray对象。它是一个JSONObject,你不能把它解析成JSONArray。有关更多参考,您可以检查it here

相关问题