【问题标题】:UnknownHostException When Host is Reachable主机可达时出现 UnknownHostException
【发布时间】:2014-09-08 05:50:40
【问题描述】:

我有一些 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 的手机配对,所以我不知道为什么它无法连接到互联网。

【问题讨论】:

  • 试试这个网址http://www.ephemeraltech.com/demo/android_tutorial20.php
  • 不幸的是没有工作:(
  • 你是高压设备还是模拟器?
  • 它在模拟器上运行
  • 进入模拟器的浏览器,检查网络是否正常?

标签: android androidhttpclient


【解决方案1】:

转到Hurl.it,如果它抛出正确的响应,你的设备互联网问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多