如何从oncreate函数调用JSONObject函数?

时间:2013-10-19 20:43:46

标签: java android

不想长时间提问。我想如何在oncreate中调用用户函数?还需要我每次都使用throw catch吗?

public class Json extends Activity {

    HttpClient client;
    TextView tvStatus;

    final static String url = "http://localhost/index.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.json);

        tvStatus = (TextView) findViewById(R.id.tvStatus);
        client = new DefaultHttpClient();

    }

    public JSONObject users() throws ClientProtocolException, IOException
    {
        StringBuilder URL = new StringBuilder();
        HttpGet get = new HttpGet(url);
        HttpResponse r = client.execute(get);

        int status = r.getStatusLine().getStatusCode();
        tvStatus.setText(status);

        return null;        
    }
}

1 个答案:

答案 0 :(得分:1)

由于还没有完整的堆栈跟踪,我只是猜测。问题是你在主线程上执行的东西。自Android 4.0以来,您需要在后台执行它。

AsynTask,你可以简单地创建一个类并让它继承。 Check this out for more infos