org.json.JSONException没有收费数据

时间:2014-09-18 19:05:19

标签: android json android-asynctask

我从mysql bd到android app的调用数据有这个问题,这是主要的活动

公共类CargaPrincipal扩展了ActionBarActivity {

ListView listaJSON;
//ArrayList<Empresa> empresaAvaiable = new ArrayList<Empresa>();
//Empresa emp;

@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.home);

listaJSON = (ListView) findViewById(R.id.lv_empresas);
//ArrayList<empresa> empresaAvaiable = new ArrayList<empresa>();
Tarea1 tarea1 = new Tarea1();
tarea1.cargarContenido(getApplicationContext());
//tarea1.onPreExecute();
tarea1.execute(listaJSON);

}

static class Tarea1 extends AsyncTask<ListView, Void, ArrayAdapter<Empresa>>{

    Context contexto;
    ListView list;
    InputStream is;
    ArrayList<Empresa> listaempresas = new ArrayList<Empresa>();



public void cargarContenido(Context contexto){

    this.contexto = contexto;

}

@Override
public void onPreExecute(){

    //vacio

}

@Override
protected ArrayAdapter<Empresa> doInBackground(ListView... params){

    list = params[0];
    String resultado = "fallo";
    Empresa emp;

    HttpClient cliente = new DefaultHttpClient();
    HttpGet peticionGet = new HttpGet("http://xxxxxxxx/xxxx/xxxxxxxx.php");
    try {
        HttpResponse response = cliente.execute(peticionGet);
        HttpEntity contenido = response.getEntity();
        is = contenido.getContent();
    } catch (ClientProtocolException e) {
        // TODO: handle exception
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    }

    BufferedReader buferlector = null;
    try {
        buferlector = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    StringBuilder sb = new StringBuilder();
    String linea = null;
    try {
        while((linea = buferlector.readLine()) != null ){
            sb.append(linea);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    try {
        is.close();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    resultado = sb.toString();

    try {
        JSONArray arrayJson = new JSONArray(resultado);
        for(int i = 0; i < arrayJson.length(); i++){
            JSONObject objetoJson = arrayJson.getJSONObject(i);
            emp = new Empresa(objetoJson.getInt("id_empresa"), objetoJson.getString("desc_empresa"));
            //emp.setData(objetoJson.getString("img_empresa"));
            listaempresas.add(emp);
        }
    } catch (JSONException e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    ArrayAdapter<Empresa> adaptador = new ArrayAdapter<Empresa>(contexto, android.R.layout.simple_list_item_1, listaempresas);

    return adaptador;

}

@Override
protected void onPostExecute(ArrayAdapter<Empresa> result){

    list.setAdapter(result);

}

@Override
protected void onProgressUpdate(Void... values){

    //vacio

}


}

这是日志猫;

org.json.JSONException: Value <pre>!DOCTYPE of type java.lang.String cannot be converted to JSONArray</pre>
org.json.JSON.typeMismatch(JSON.java:111)
org.json.JSONArray.<init>(JSONArray.java:91)
org.json.JSONArray.<init>(JSONArray.java:103)    com.siont.divi.CargaPrincipal$Tarea1.doInBackground(CargaPrincipal.java:120)
com.siont.divi.CargaPrincipal$Tarea1.doInBackground(CargaPrincipal.java:1)
android.os.AsyncTask$2.call(AsyncTask.java:287)
java.util.concurrent.FutureTask.run(FutureTask.java:234)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
java.lang.Thread.run(Thread.java:856)

我没有意识到问题是什么,我在服务器上给予权限

1 个答案:

答案 0 :(得分:0)

正如LogCat所示,似乎resultado变量包含错误的文本,而不是Json数组格式,因此必须以这种方式“[.....]”。尝试在转换前验证此变量。