httpclient (phpmyadmin) not working on Android 4.0+

前端 未结 4 1052
北恋
北恋 2021-01-27 09:12

I use this code below, it works perfectly in Android 2.3.3. However, in 4.0+ it can\'t connect to database somehow. I saw some posts about you need to get it in a asynch class.

相关标签:
4条回答
  • 2021-01-27 09:31

    Since Android 3.0 you are not allowed to do network stuff on the main thread. Why? because network problems will lead to a slow ui. So you have to do all the http stuff in a new thread. You are on the right path but you made a mistake in your AsyncTask. Delete the empty doInBackground method in you async task and write @Override over your method.

    0 讨论(0)
  • 2021-01-27 09:36

    android.os.NetworkOnMainThreadException

    this eror comes With HoneyComb(3.0 or Later). you can not perform a networking operation on its main thread as documentation says. to getting ride of this you must use handler or asynctask. AFAIK There is no another way to do it.

    you can See this for More Details WHY ICS Crashes your App

    Try Using Below Code Snippet

    new Thread(){
        public void run(){
            //do your Code Here    
        }
    }.start();
    
    0 讨论(0)
  • 2021-01-27 09:36

    why you are passing null in function of web connection and web service .?

    getServerData(null);
    
    0 讨论(0)
  • 2021-01-27 09:58

    Ok right... After searching for few hours, making this question, then 10 minutes later, you find a solution...

    Option 1:
    I added this line:

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
     StrictMode.setThreadPolicy(policy);
    

    But I reccomend NOT to use option 1, this is a bad solution for real. Use option 2!
    //===========================================================================
    Option 2:

    Used this tutorial to make a proper ASyncTask: http://www.elvenware.com/charlie/development/android/SimpleHttpGetThread.html

    //===========================================================================
    Used ASyncTask as final (option 2).

    0 讨论(0)
提交回复
热议问题