我一直在這一段時間了,我卡住了..我已經做了下面的代碼從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();
}
}
}
}
}
什麼是例外? – ThomasThiebaud
發佈printStackTrace的輸出請 – Nanoc