I've been at this for some time now, and I'm stuck.. I've made the code below to get one variable from JSON, and display it on screen in a textview. The app doesn't crash, but it always displays IT DISPLAYS THIS!!.. (see code). I can't figure out why it doesn't show the text.. It should display naam from here..
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();
}
}
}
}
}