2015-11-04 92 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线程。