主机可访问时UnknownHostException

时间:2014-09-08 05:50:41

标签: android androidhttpclient

我有一些Android代码,我希望从网页中提取数据并将其显示为文本。不幸的是,我收到了这个错误,即使它有适当的权限和所有内容:

  

java.net.UnknownHostException:无法解析主机" ephemeraltech.com":没有与主机名关联的地址

这是我的清单文件权限:

<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

以下是代码本身:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.round_activity_my);

        TextView mTextView = (TextView)findViewById(R.id.text);

        myAsyncTask mTask = new myAsyncTask();
        mTask.execute("http://ephemeraltech.com/demo/android_tutorial20.php");

        /* final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
            }
        }); */
    }

    private class myAsyncTask extends AsyncTask<String,Void,Void> {
        public String resultText;

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected Void doInBackground(String... str) {
            try {
                System.out.println("testing");
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://ephemeraltech.com/demo/android_tutorial20.php");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                InputStream webs = entity.getContent();
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);
                    resultText = reader.readLine();
                    webs.close();
                    System.out.println("testing II");
                } catch(Exception e){
                    resultText = "hi";
                }
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            return null;
        }
    }

    protected void onPostExecute(String resultText) {
        TextView mTextView = (TextView)findViewById(R.id.text);
        mTextView.setText("sdfdfdfdf");
        System.out.println("Testing III");
    }
}

我不知道为什么会得到它。有人有任何想法吗?

编辑:它是Android Wear模拟器,与配有Wifi的手机配对,因此我不知道为什么它无法上网。

1 个答案:

答案 0 :(得分:0)

如果它引发了正确的响应,您的设备上网问题

,请转到Hurl.it