Android 无法从JSON到textview获取结果
我已经做了一段时间了,我被困住了。。我编写了下面的代码,从JSON中获取一个变量,并将其显示在文本视图的屏幕上。应用程序不会崩溃,但它总是显示它显示这个!!。。(参见代码)。我不明白它为什么不显示文本Android 无法从JSON到textview获取结果,android,json,textview,Android,Json,Textview,我已经做了一段时间了,我被困住了。。我编写了下面的代码,从JSON中获取一个变量,并将其显示在文本视图的屏幕上。应用程序不会崩溃,但它总是显示它显示这个!!。。(参见代码)。我不明白它为什么不显示文本 请使用单独的线程式异步任务进行网络操作。您正在OnCreate()中使用getMethod,这会导致异常。在服务器结果中,您得到的是json数组,但当您作为json对象进行解析时,它会被删除 JSONObject json=新的JSONObject(结果) 这里的结果是一个字符串类型的json数组
请使用单独的线程式异步任务进行网络操作。您正在OnCreate()中使用getMethod,这会导致异常。在服务器结果中,您得到的是json数组,但当您作为json对象进行解析时,它会被删除 JSONObject json=新的JSONObject(结果) 这里的结果是一个字符串类型的json数组,所以您可以使用下面的代码来解析 JSONObject json=新的JSONArray(结果).get(0)
最后也是最重要的一点是,您应该使用AsynkTask调用web服务或长操作,否则它将阻塞您的UI线程。有什么例外?请发布printStackTrace的输出。他们应该在不同的线程而不是UI线程上执行工作,但不要使用异步任务,尤其是如果他们想更新用户界面。要更新textview或任何UI,您需要保留对有效上下文的引用。但是现在考虑配置改变发生了,没有有效的上下文给你,这就是你的漏洞。如果您尝试使用async task触发网络请求,并且在该过程中您决定旋转屏幕(即配置更改),则该网络请求将说再见。您可以在onPostExexcute()或onProgressUpdate()中更新AsyncTask中的UI组件在AsyncTask的UI线程上执行。因此使用AsyncTask不是问题。关于配置更改,请给我一些时间,我会给你回复的。请使用帖子中提到的方法进行配置更改。
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();
}
}
}
}
}