ERRORE JSON "Valore al risultato del tipo java.lang.string non può essere convertito in jsonarray" in Android

StackOverflow https://stackoverflow.com/questions/8906969

  •  29-10-2019
  •  | 
  •  

Domanda

Ho usato il collegamento Per JSON RPC. Sto ricevendo una risposta come previsto. Ma quando provo a analizzare la risposta, mi sta dando un errore JSON.

Il mio codice:

JSONEntity entity = new JSONEntity(jsonRequest);
    HttpPost request = new HttpPost("http://192.168.1.150/jsondemo12/service.asmx");
    request.setEntity(entity);
    HttpResponse response = httpClient.execute(request);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity httpEntity = response.getEntity();
        InputStream content = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(content,"iso-8859-1"),8);
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        content.close();
    } else {
        Log.e(AndroidJSONActivity.class.toString(), "Failed to download file");
    }


    strJSONValue=builder.toString();

    txtViewParsedValue.append("\n+++++++++++++\n"+strJSONValue+"\n");
    try {
        parseJSON();
    } catch (JSONException e) {
        Log.e("error","Error while parsing!!!");
        e.printStackTrace();
    }
    Log.e("response", strJSONValue);
public void parseJSON() throws JSONException
    {
        String attr1="",attr2="";
        jsonObject = new JSONObject(strJSONValue);
        JSONArray  result = jsonObject.getJSONArray("result");    <- Error in this line!!!
        for(int i=0;i < result.length();i++){
            JSONObject e = result.getJSONObject(i);
            attr1 = "ExhibitorID: "+ e.getString("ExhibitorID");
            attr2 = "ExhibitorName: "+e.getString("ExhibitorName");
        }
        strParsedValue=attr1+"\n"+attr2;
        Log.d("Parse", attr1);
        Log.d("Parse", attr2);

        txtViewParsedValue.append("\n**********\nParsed Value: \n");
        txtViewParsedValue.append(strParsedValue);
    }

Il risultato che ottengo in "StrjsonValue" è il formato stringa, senza le doppie citazioni di avvio e fine. Piace:

{"id":2,"result":"[
{\"ExhibitorID\":42, etc....}
]"}

La stringa dei risultati è secondo requisito, ma non sono in grado di analizzare la stringa nell'oggetto JSON secondo requisito. Dà errore in Logcat: org.json.JSONException: Value <content of the string> at result of type java.lang.String cannot be converted to JSONArray

Mi aiuti per favore. Grazie

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top