2014-09-18 29 views
0

我對從MySQL到BD Android應用呼叫數據這個問題,這是在MainActivityorg.json.JSONException不收費數據

公共類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) 

我不知道什麼問題,我給在服務器上

回答

0

作爲logcat的節目的權限,這似乎不是JSON數組格式的resultado變量包含錯誤的文本,女巫必須是這樣「[.....]」。嘗試在轉換前驗證此變量。

+0

問題出在你說的那條車道上,我試圖修復,但是除非,你有什麼想法嗎? – jhon 2014-09-18 21:59:53