Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 每次我发出http请求时,应用程序都会崩溃_Android_Http_Crash_Request - Fatal编程技术网

Android 每次我发出http请求时,应用程序都会崩溃

Android 每次我发出http请求时,应用程序都会崩溃,android,http,crash,request,Android,Http,Crash,Request,这是我的密码 每次我触摸imageview时,我的应用程序都会等待大约5秒,然后停止 我有上网许可 在服务器端,我有一个php页面,读取GET并将其插入数据库 public class Home extends Activity { ImageView lightbut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCon

这是我的密码 每次我触摸imageview时,我的应用程序都会等待大约5秒,然后停止

我有上网许可

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

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
上做网络工作。您应该将所有网络代码移动到后台
线程
,例如
异步任务
。这将使您能够轻松地在后台执行网络操作,然后根据需要在
UI
上运行的函数中更新
UI

这显示了基本结构。基本上,您可以从
UI
中调用
AsyncTask
,例如在
onClick()
中,然后在
doInBackground()
中执行网络操作,该操作在任务首次启动时调用,然后您可以使用其任何其他方法更新
UI

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

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 
}

我该怎么做?如果你给我一个密码,我将非常感激!我编辑了一个链接,链接到一个开始使用它的示例。你也应该阅读文档,因为你需要了解一些重要的信息。我再次编辑了一些描述。这应该让你开始。如果在其他任何地方都不需要,您只需将
AsyncTask
作为
活动的内部类即可。然后,您可以访问
活动的成员变量
,也可以访问其功能。为了便于使用,请签出。获取
XML
JSON
就像
$.getJSON(url)
$.getXML(url)
一样简单。现在你应该使用Android的凌空库