我正在尝试在从json数组中提取的textview中显示数据。
我得到正确的服务器答案,第一个toast正确显示de json array {“android”:{“total”:4}}
php页面在线http://www.2w.cl/apps/db/cargar_resultados.php
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class SeeResults extends Activity{
TextView texto;
JSONArray android = null;
JSONParser jsonParser = new JSONParser();
private static String url = "http://www.2w.cl/apps/db/cargar_resultados.php/";
private static final String TAG_OS = "android";
private static final String TAG_CONSULTA = "total";
public String idconsulta;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.see_results);
idconsulta = getIntent().getStringExtra("test");
TextView tv = null;
tv = (TextView)findViewById(R.id.textView1);
tv.setText(idconsulta);
Toast.makeText(this, "On Create Called" + idconsulta, Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
new JSONParse().execute();
Toast.makeText(this, "On Start Called", Toast.LENGTH_LONG).show();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("idconsultas", idconsulta));
JSONObject json = jsonParser.makeHttpRequest(url, "POST", params);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
Toast.makeText(getBaseContext(), "test" + json, Toast.LENGTH_LONG).show();
try {
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
String total = c.getString(TAG_CONSULTA);
Toast.makeText(getBaseContext(), "test2" + total, Toast.LENGTH_LONG).show();
TextView tv = null;
tv = (TextView)findViewById(R.id.textView1);
tv.setText(total);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
toast“test2”不显示任何内容。
感谢您的帮助
答案 0 :(得分:0)
这是未经测试的,但请注意以下代码:
String result = "{\"android\":{\"total\":\"4\"}}";
Log.d("something", "result: " + result);
try {
JSONObject hold = new JSONObject(result);
JSONObject obj = hold.getJSONObject("android");
Log.d("something", "total: " + obj.getString("total"));
} catch (JSONException e) {
Log.d("something", "ERROR");
}