Android:将数据发送到php时出错

时间:2015-07-26 16:02:31

标签: android

我使用Send类向php发送电子邮件但是出错

private class Send extends AsyncTask<String, String, String> {

    private InputStream is = null;
    private String url = "http://*****.com/sand.php";
    private String page_output = "";

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

            try {
                    // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Email", ed_email.getText().toString()));

            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

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

            Log.i("Ekhteraat app ControlPanel", "page_output --> " + page_output); ///<--------------------------|
        } 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 {

            myJSON=page_output;


        } catch (Exception e) {
            e.printStackTrace();
        }
   }
} 

这是日志中的错误

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

1 个答案:

答案 0 :(得分:1)

将此添加到AndroidManifest.xml

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <application ...
</manifest>

这有帮助吗?