不想长时间提问。我只是想如何在oncreate内调用用户函数?还需要我每次都使用丢球吗?

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;
    }
}

最佳答案

由于还没有完整的堆栈跟踪信息,所以我会猜测。问题是您在主线程上执行了东西。从Android 4.0开始,您需要在后台执行它。

为此,您只需创建一个类并让其继承即可。 Check this out for more infos

09-27 03:11