我想从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
答案 0 :(得分:2)
请注意错误消息:
引起:java.lang.SecurityException:权限被拒绝(缺失 INTERNET许可?)
您错过了权限:
<uses-permission android:name="android.permission.INTERNET" />
在打开<application>
标记之前将其添加到清单文件中。
答案 1 :(得分:0)
请在Manifest中添加权限。
<uses-permission android:name="android.permission.INTERNET" />