Android:从mysql获取数据时doInBackground出错

Posted

技术标签:

【中文标题】Android:从mysql获取数据时doInBackground出错【英文标题】:Android:error in doInBackground when get data from mysql 【发布时间】:2016-03-25 23:30:54 【问题描述】:

我想从 mysql 表中获取数据,我使用 doInBackground 但出错了

这是我的代码:

    private class getStatistics extends AsyncTask<String, String, String> 

        private InputStream is = null;
        private String url = "http://xxx/get_statistics.php";
        private String page_output = "";


        @Override
        protected String doInBackground(String... args) 

            Log.i("shis app ControlPanel", "Start get all Statistics ");

            try 
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
             catch (UnsupportedEncodingException e) 
                e.printStackTrace();
             catch (ClientProtocolException e) 
                e.printStackTrace();
             catch (IOException e) 
                e.printStackTrace();
            
            try 
                Log.i("shis app ControlPanel", "3");
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                
                    sb.append(line + "\n");
                
                is.close();
                page_output = sb.toString();
             catch (Exception e) 
                Log.e("Buffer Error", "Error converting result " + e.toString());
            

            return page_output;
        

        @Override
        protected void onPostExecute(String page_output) 
                //pDialog.dismiss();
            try 

                statJSON=page_output;

                if(Integer.parseInt(page_output.trim())>=0)
                    statAll=Integer.parseInt(statJSON.trim());
                

             catch (Exception e) 
                e.printStackTrace();
            
       
   

这是错误:

12-19 04:50:37.513: E/androidRuntime(992): FATAL EXCEPTION: AsyncTask #1
12-19 04:50:37.513: E/AndroidRuntime(992): java.lang.RuntimeException: An error occured while executing doInBackground()
12-19 04:50:37.513: E/AndroidRuntime(992):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-19 04:50:37.513: E/AndroidRuntime(992):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.lang.Thread.run(Thread.java:841)
12-19 04:50:37.513: E/AndroidRuntime(992): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-19 04:50:37.513: E/AndroidRuntime(992):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-19 04:50:37.513: E/AndroidRuntime(992):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-19 04:50:37.513: E/AndroidRuntime(992):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-19 04:50:37.513: E/AndroidRuntime(992):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-19 04:50:37.513: E/AndroidRuntime(992):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-19 04:50:37.513: E/AndroidRuntime(992):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-19 04:50:37.513: E/AndroidRuntime(992):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-19 04:50:37.513: E/AndroidRuntime(992):  at com.example.shispanel.SplashActivity$getStatistics.doInBackground(SplashActivity.java:67)
12-19 04:50:37.513: E/AndroidRuntime(992):  at com.example.shispanel.SplashActivity$getStatistics.doInBackground(SplashActivity.java:1)
12-19 04:50:37.513: E/AndroidRuntime(992):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-19 04:50:37.513: E/AndroidRuntime(992):  ... 4 more
12-19 04:50:37.513: E/AndroidRuntime(992): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-19 04:50:37.513: E/AndroidRuntime(992):  at libcore.io.Posix.getaddrinfo(Native Method)
12-19 04:50:37.513: E/AndroidRuntime(992):  at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
12-19 04:50:37.513: E/AndroidRuntime(992):  at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
12-19 04:50:37.513: E/AndroidRuntime(992):  ... 17 more
12-19 04:50:37.513: E/AndroidRuntime(992): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
12-19 04:50:37.513: E/AndroidRuntime(992):  ... 20 more

【问题讨论】:

【参考方案1】:

注意错误信息:

原因:java.lang.SecurityException: Permission denied (missing 互联网权限?)

您缺少权限

<uses-permission android:name="android.permission.INTERNET" /> 

将其添加到您的 ma​​nifest 文件中,在开始 &lt;application&gt; 标记之前。

【讨论】:

【参考方案2】:

请在 Manifest 中添加权限。

<uses-permission android:name="android.permission.INTERNET" />

【讨论】:

以上是关于Android:从mysql获取数据时doInBackground出错的主要内容,如果未能解决你的问题,请参考以下文章

从 Mysql 数据库获取数据到 Android

位置提供者=null,安卓

使用php从mysql获取数据到android

如何从mysql中获取数据并存储在android的Sqlite数据库中

在Android中使用JDBC MySQL连接数据库?

如何从存储在数据库中的 Listview 获取值到另一个活动?使用 Android 和 Mysql