1

不想问太久。只是我想如何在 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;        
    }
}
4

1 回答 1

1

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

为此,您只需创建一个类AsynTask并让它继承。查看此以获取更多信息

于 2013-10-19T20:46:39.157 回答