2015-11-04 122 views
2

我一直在這一段時間了,我卡住了..我已經做了下面的代碼從JSON獲取一個變量,並顯示在屏幕上的文本視圖。該應用程序不會崩潰,但它始終顯示IT DISPLAYS THIS !! ..(看代碼)。我想不通爲什麼它不顯示文本.. It should display naam from here..從JSON到textview沒有得到結果

package me.janvandijk.receptenapp; 


public class Recept extends Activity { 

    String receptid; 
    String result; 
    TextView receptTitel; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_recept); 

    receptid = getIntent().getStringExtra("receptid"); 
    receptTitel = (TextView)findViewById(R.id.receptTitel); 

    //First Initialise TextView 
    getMethod method=new getMethod(); 
    try { 
     result = method.getInternetData();// Then get the Json response 
    } catch (Exception e) { 
     receptTitel.setText("IT DISPLAYS THIS!!.."); 
     e.printStackTrace(); 
    } 

    try{ 
     JSONObject json=new JSONObject(result); 
     try { 
      String receptNaam =json.getString("naam"); 
      receptTitel.setText(receptNaam); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      receptTitel.setText("Never Seen This.."); 
     } 

    } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //new ReceptAsynTask().execute("http://janvandijk.me/zooi/receptenapp/getrecipes.php?type=request&datatype=recept&id=" + receptid); 



    public void setText(String string){ 
     //tv.setText(string); 
    } 

    public void showToast(){ 
     Toast.makeText(getApplicationContext(), "Bezig met laden recept met ID "+receptid, Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_recept, menu); 
     //TextView view = (TextView) findViewById(R.id.testje); 
     //view.setText(receptid); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 


public class getMethod { 

    public String getInternetData() throws Exception { 
     BufferedReader in = null; 
     String data = null; 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      URI website = new URI("http://janvandijk.me/zooi/receptenapp/getrecipes.php?type=request&datatype=recept&id=1"); 

      HttpGet request = new HttpGet(); 
      request.setURI(website); 
      HttpResponse response = client.execute(request); 
      in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
      StringBuffer sb = new StringBuffer(""); 
      String l = ""; 
      String nl = System.getProperty("line.separator"); 
      while ((l = in.readLine()) !=null) { 
       sb.append(l + nl); 
      } 

      in.close(); 
      data = sb.toString(); 
      return data; 
     } 
     finally { 
      if (in !=null){ 
       try{ 
        in.close(); 
        return data; 
       }catch (Exception e){ 
        e.printStackTrace(); 

       } 
      } 

     } 
    } 
} 
+0

什麼是例外? – ThomasThiebaud

+0

發佈printStackTrace的輸出請 – Nanoc

回答

0

請使用單獨的線程像的AsyncTask網絡操作。您在OnCreate()中使用getMethod會導致異常。

+0

授予,他們應該在不同的線程而不是UI線程上工作,但不使用異步任務,特別是如果他們想更新UI。要更新文本視圖或任何用戶界面,您需要持有對有效上下文的引用。但現在考慮一下配置更改發生,再也沒有有效的背景了,這就是你如何泄漏。如果您嘗試使用異步任務來觸發網絡請求,並且正在進行,則您決定旋轉屏幕(即配置更改),該網絡請求將告別。 –

+0

您可以通過在AsyncTask的UI線程上執行的onPostExexcute()或onProgressUpdate()來更新AsyncTask中的UI組件。因此,使用AsyncTask並不是問題。來配置變化,給我一些時間,我會回到你身邊。 –

+0

http://stackoverflow.com/q/7128670/4476794請使用後面提到的方法進行配置更改。 –

0

在您的服務器結果你得到JSON數組但是當你解析爲JSON對象,它

JSONObject的JSON =新的JSONObject(結果);

這裏你的結果是字符串類型JSON數組因此可以使用以下代碼來解析

的JSONObject JSON =新JSONArray(結果)獲得(0);

最後一點也很重要,您應該使用AsynkTask來調用Web服務或長時間操作,否則它會阻止您的UI線程。