2014-12-02 80 views
1

我在一个主机使用mysql数据库,我想从我的主机recive数据,但是当我得到的数据到一个textview,它是空白的。php json得到一个空白textview

public class MainActivity extends Activity { 
TextView tv1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tv1=(TextView)findViewById(R.id.name); 
    Loader loader = new Loader(getApplicationContext()); 
    tv1.setText(loader.loadInBackground()); 
} 
public static class Loader extends AsyncTaskLoader<String>{ 
    public Loader (Context contexto) 
    { 
     super(contexto); 

    } 

    @Override 
    public String loadInBackground() { 

     String resultado = ""; 
     String entrada = ""; 

     DefaultHttpClient cliente = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet("http://comupunt.esy.es/cities.php"); 
     try { 
      HttpResponse execute = cliente.execute(httpget); 
      InputStream contenido = execute.getEntity().getContent(); 
      BufferedReader buffer = new BufferedReader(new InputStreamReader(contenido)); 

      String s=""; 
      while((s=buffer.readLine())!=null) 
       resultado+=s; 
      { 

      } 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 

     try { 

      JSONObject object = new JSONObject(resultado); 
      JSONArray jarray = object.getJSONArray("cities"); 

      for (int i=0;i<jarray.length();i++) 
      { 
       JSONObject jobject = jarray.getJSONObject(i); 
       String name=jobject.getString("name"); 
       entrada += name; 

      } 


     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("WebService", e.getMessage()); 
     } 

     return entrada; 

    } 
} 


} 

使用JSON的PHP得到这个

{ “城市”:[{ “Name”: “的Aitor”}]}

和logcat的:

12-02 04:24:01.492: E/WebService(2190): End of input at character 0 of 

感谢

+0

的可能的复制http://stackoverflow.com/questions/24301521/jsonexception-end-of-input-at-character-0或http://stackoverflow.com/questions/ 23170610/error-parsing-data-org-json-jsonexception -in-input-at-character-0- – Chitrang 2014-12-02 05:10:56

+0

我只在logcat中出错 – 2014-12-02 05:14:28

回答

1

你在MainActivity中这么行

HttpResponse execute = cliente.execute(httpget); 

抛出networkonmainthreadexception您可以通过使用检查(在第一次尝试捕捉以及)

catch (Exception e) { 
     // TODO: handle exception 
     Log.e("errormsg", e.ToString(); 
    } 

因此而不是使用AsyncTaskLoader更好地使用的AsyncTask的。

public class MainActivity extends Activity { 

TextView tv1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tv1 = (TextView) findViewById(R.id.text); 
    Exe exe = new Exe(); 
    try { 
     URI uri = new URI("http://comupunt.esy.es/cities.php"); 
    } catch (URISyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    exe.execute(); 

} 

class Exe extends AsyncTask<URL, String, String> { 

    String entrada = ""; 

    @Override 
    protected String doInBackground(URL... url) { 

     String resultado = ""; 

     try { 
      DefaultHttpClient cliente = new DefaultHttpClient(); 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      URI uri = new URI("http://comupunt.esy.es/cities.php"); 
      HttpGet http = new HttpGet(uri); 


      HttpResponse execute = cliente.execute(http); 

      InputStream contenido = execute.getEntity().getContent(); 

      BufferedReader buffer = new BufferedReader(
        new InputStreamReader(contenido)); 

      String s = ""; 

      while ((s = buffer.readLine()) != null) { 
       resultado += s; 
      } 
      JSONObject object = new JSONObject(resultado); 
      Log.e("WebService1", object.toString()); 
      JSONArray jarray = object.getJSONArray("cities"); 

      for (int i = 0; i < jarray.length(); i++) { 
       JSONObject jobject = jarray.getJSONObject(i); 
       String name = jobject.getString("name"); 
       entrada = name; 

      } 

     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("WebService", e.getMessage()); 
     } 

     return entrada; 

    } 


    @Override 
    protected void onPostExecute(String res) { 

     tv1.setText(entrada); 
    } 
} 

}

+0

非常感谢你!我已经做到了!但是,我可以从数据库中填充更多数据的对象吗? – 2014-12-02 13:01:11

+0

我可以用一行一行地填充不同数据的列表视图?像这样:姓名和年龄_______姓名和年龄 – 2014-12-02 13:21:00