应用程序崩溃每次我做一个HTTP请求 [英] App crashes every time i make an http request

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

问题描述

这是我的code 每次我摸到了ImageView的我的应用程序等待大约5秒,然后chrashes

我有INTERNET权限

在服务器端我有一个PHP页面读取GET和在数据库中插入

 公共类家庭延伸活动{
ImageView的lightbut;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_home);
    ImageView的lightbut =(ImageView的)findViewById(R.id.light);
    lightbut.setClickable(真正的);
    lightbut.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            Log.d(==我的活动===,的OnClick被称为);
         //创建HTTP客户端
            HttpClient的HttpClient的=新DefaultHttpClient();

            //创建HTTP发布
            HTTPGET httpPost =新HTTPGET(http://192.168.0.102/HR/index.php?command=ligthsoff);
            尝试 {
                HTT presponse响应= httpClient.execute(httpPost);


            }赶上(ClientProtocolException E){
                //写入例外日志
                e.printStackTrace();

            }赶上(IOException异常E){
                //写入例外日志
                e.printStackTrace();
            }
        }
    });
}
 

解决方案

一个logcat的将是非常有益的,但​​它可能是从做网络上的东西了 UI 。你应该将所有的网络code到背景的AsyncTask 。这将很容易让你做网络的东西在后台然后更新 UI 如果需要在对 UI运行功能

AsyncTask的文档

<一个href="http://stackoverflow.com/questions/15455858/using-asynctask-to-speed-up-android-app-launch-time/15456047#15456047">Here一个答案的显示的基本结构。基本上,你叫的AsyncTask UI 如您的onClick()则在 doInBackground()任务的时候第一次启动,那么你可以更新这就是所谓的 UI 在其任何其他方法。

使用我引用的例子中,你只需把你的所有网络的东西,它看起来像一切都在你的的onClick(),里面的在链接的例子doInBackground()方法。然后在你的的onClick(),你会做这样的事情

  lightbut.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
       TalkToServer任务=新TalkToServer(); //可以传递PARAMS这里构造器,但可能不需要在这种情况
       task.execute(); //可以传递PARAMS为`doInBackground()`如URL,等...但同样可能不需要这种情况
}
 

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

I have the INTERNET permission

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

解决方案

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 Docs

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.

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天全站免登陆