每次我发出 http 请求时应用程序都会崩溃 [英] App crashes every time i make an http request

查看:30
本文介绍了每次我发出 http 请求时应用程序都会崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码每次我触摸图像视图时,我的应用程序都会等待大约 5 秒然后崩溃

This is my code every time i touch the imageview my app waits about 5 secs and then chrashes

我有互联网权限

在服务器端,我有一个 php 页面,它读取 GET 并将其插入到数据库中

On the server side i have a php page that reads the GET and insert it in a database

public class Home extends Activity {
ImageView lightbut;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ImageView lightbut = (ImageView) findViewById(R.id.light);
    lightbut.setClickable(true); 
    lightbut.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("== My activity ===","OnClick is called");
         // Creating HTTP client
            HttpClient httpClient = new DefaultHttpClient();

            // Creating HTTP Post
            HttpGet httpPost = new HttpGet("http://192.168.0.102/HR/index.php?command=ligthsoff");
            try {
                HttpResponse response = httpClient.execute(httpPost);


            } catch (ClientProtocolException e) {
                // writing exception to log
                e.printStackTrace();

            } catch (IOException e) {
                // writing exception to log
                e.printStackTrace();
            }
        }
    });
}

推荐答案

logcat 会很有帮助,但它可能来自在 UI 上处理网络问题.您应该将所有网络代码移动到后台 Thread,例如 AsyncTask.这将允许您轻松地在后台执行网络操作,然后根据需要在 UI 上运行的函数中更新 UI.

A logcat would be very helpful but its probably from doing network stuff on the UI. You should move all of your network code to a background Thread such as an AsyncTask. This will easily allow you to do the network stuff in the background then update the UI if needed in functions that run on the UI.

AsyncTask 文档

这里有答案 显示基本结构.基本上,您从 UI 调用 AsyncTask,例如在您的 onClick() 中,然后您在 doInBackground()<中执行网络操作/code> 在任务首次启动时调用,然后您可以在任何其他方法中更新 UI.

Here is an answer that shows the basic structure. Basically, you call the AsyncTask from the UI such as in your onClick() then you do network operations in doInBackground() which is called when the task first starts then you can update the UI in any of its other methods.

使用我引用的示例,您只需将所有网络内容(看起来就像 onClick() 中的所有内容)放在 doInBackground() 方法中链接中的示例.然后在您的 onClick() 中,您将执行类似

Using the example I referenced, you would just put all of your network stuff, which looks like everything in your onClick(), inside the doInBackground() method of the example in the link. Then in your onClick() you would do something like

lightbut.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       TalkToServer task = new TalkToServer(); // could pass params to constructor here but might not be needed in this situation
       task.execute();  // could pass in params for `doInBackground()` such as url, etc... but again maybe not needed for this situation 
}

这篇关于每次我发出 http 请求时应用程序都会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆