2014-12-02 2 views
1

호스트에서 mysql 데이터베이스를 사용하고 있습니다. 호스트에서 데이터를 받기를 원하지만 텍스트 뷰로 데이터를 가져올 때 비어 있습니다.php Json 빈 텍스트보기를 받고

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이

얻을 { "도시": [{ "이름": "토르"}]}

과 로그 캣 :

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-of-0-of-character-0- – Chitrang

+0

나는 logcat에 오류가 있습니다. –

답변

1

당신은 MainActivity에 있습니다. 따라서 라인은

입니다.
HttpResponse execute = cliente.execute(httpget); 

은 AsyncTask를를 사용하는 것이 더 AsyncTaskLoader 사용하는 그래서 대신 (물론 첫 번째 시도 캐치에) 사용하여 확인할 수 있습니다

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

을 networkonmainthreadexception을 던지고있다.

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

대단히 감사합니다 !! 나는 그것을 가지고있다!! 그러나 데이터베이스에서 더 많은 데이터 객체를 채울 수 있습니까? –

+0

행별로 서로 다른 데이터로 listview를 채울 수 있습니까? 이런 이름과 나이 _______ 이름과 나이 –