我已经有一段时间了,我已经卡住了..我已经制作了下面的代码,从JSON获取一个变量,并在文本视图中显示在屏幕上。该应用程序不会崩溃,但它始终显示 IT DISPLAYS THIS !! .. (参见代码)。我无法弄清楚为什么它没有显示文字.. 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();
}
}
}
}
}
答案 0 :(得分:0)
请使用AsyncTask等单独的线程进行网络操作。您在OnCreate()中使用getMethod会导致异常。
答案 1 :(得分:0)
在您的服务器结果中,您将获得json数组,但是当您将其解析为json对象时
JSONObject json = new JSONObject(result);
这里你的结果是一个字符串类型的json数组,所以你使用下面的代码来解析
JSONObject json = new JSONArray(result).get(0);
最后也很重要的是你应该使用AsynkTask来调用Web服务或长时间操作,否则会阻塞你的UI线程。