简体   繁体   English

ICS上的HttpUrlConnection应用程序崩溃

[英]HttpUrlConnection App Crashes on ICS

The method sendMessage(View view) runs on clicking a button. sendMessage(View view)方法在单击按钮时运行。 The method is being called successfully because t.setText("Checking..."); 该方法被成功调用,因为t.setText("Checking..."); changes the text to Checking... as expected. 将文本更改为“ Checking... ”。

The issue is that the app force closes when s= urlConnection.getResponseCode(); 问题是当s= urlConnection.getResponseCode();时,应用程序强制关闭s= urlConnection.getResponseCode(); is not commented out. 没有被注释掉。 Is it because a connection is not being established or have I missed out something in code? 是因为未建立连接还是我错过了代码中的某些内容?

I am new to android programming and couldn't find a proper guide or video tutorial on web services using HttpUrlConnection. 我是android编程的新手,无法使用HttpUrlConnection在Web服务上找到合适的指南或视频教程。 I would like to learn this before moving on to REST, SOAP or other APIs. 在继续使用REST,SOAP或其他API之前,我想学习一下。

What have I tried? 我尝试了什么?

  1. Changed URL to a sub-domain I own but it still crashes. 将URL更改为我拥有的子域,但仍然崩溃。

  2. Searched on forums and StackOverflow and came across something called AsyncTask. 在论坛和StackOverflow上进行搜索,发现了一个叫做AsyncTask的东西。 I am not sure if that's necessary here because I didn't find that as a requirement in Android Doc. 我不确定在这里是否有必要,因为我在Android Doc中没有发现这是必需的。 I learnt most of the connection part from Android Doc since video tutorials didn't explain well. 由于视频教程的讲解不佳,因此我从Android Doc学习了大部分的连接部分。

    public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

    }
    public void sendMessage(View view) throws IOException {

        TextView t=findViewById(R.id.hello2);
        t.setText("Checking...");
        URL url = new URL("http://google.com");
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {

            int s=0;
            s= urlConnection.getResponseCode();
            t.setText("Nice.");
        } finally {
            urlConnection.disconnect();
        }
     }
}

I am aware that things get easier with APIs like REST and SOAP but I would like to do it this way for learning purposes. 我知道使用REST和SOAP这样的API可以使事情变得更容易,但是我希望以此方式进行学习。 It would be awesome if someone could guide me to a proper tutorial or blog about the same. 如果有人可以指导我找到适当的教程或博客,那将是非常棒的。 I honestly couldn't find anything properly explained in the tutorials I referred. 老实说,我找不到我所介绍的教程中适当解释的任何内容。

For sure you can't do that in this way, Why? 当然不能以这种方式这样做,为什么?

Because of Main Thread in android which is UI Thread, it can't do long time operation like network threads, it can't wait for it, however any explicit thread doesn't have access to the UI Thread then what should you do ? 由于Android中的Main Thread是UI Thread,它无法像网络线程一样进行长时间的操作,它无法等待,但是任何显式线程都无法访问UI Thread,那该怎么办?

You have to make Async Method and The httpUrlConnection should be called inside Class called AsyncTask and if you do really need to edit some component in UI thread while you are inside async method then You have to use Handlers in order to edit some of them, so your code should work only if you did something like that 您必须创建异步方法,并且应该在名为AsyncTask的类内部调用httpUrlConnection ,如果确实需要在异步方法内部编辑UI线程中的某些组件,则必须使用处理程序来编辑其中的一些组件,因此您的代码只有在执行了类似的操作后才可以工作

new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground( Void... voids ) {
        HttpURLConnection urlConnection = (HttpURLConnection) 
        url.openConnection();
        return null;
    }
}.execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM