【问题标题】:Why I can't access my escaped URL?为什么我无法访问我的转义 URL?
【发布时间】:2015-02-06 12:54:30
【问题描述】:

我正在尝试在AsyncTask 中向我的服务器发送一些参数,但没有任何运气。

这是我的代码:

try {
                Uri.Builder builder = new Uri.Builder();
                builder.scheme("https")
                        .authority("www.dummyurl.com/android/agent.php")
                        .appendQueryParameter("date", selectedDate.getText().toString()+" "+ selectedTime.getText().toString())
                        .appendQueryParameter("user", userTxt.getText().toString())
                        .appendQueryParameter("cal", calTxt.getText().toString());
                String myUrl = builder.build().toString();

                System.out.println(myUrl);

                URL url = new URL(myUrl);

                URLConnection urlConnection = url.openConnection();
                InputStream in = urlConnection.getInputStream();
            } catch (Exception e) {
                e.printStackTrace();
            }

myUrl 看起来像:https://www.dummyurl.com%2Fandroid%2Fagent.php?date=February-6-2015%2014%3A49&user=qwe&cal=qweqwe

但我收到以下错误:

java.net.UnknownHostException: host == null

我知道由于某种原因我无法连接到转义的 URL,我知道我把事情搞砸了,但是作为一个 android 初学者,我真的找不到向我的 URL 发出请求的方法。我知道我在这里遗漏了一小部分,但我目前无法发现它。

【问题讨论】:

    标签: java android url android-asynctask uri


    【解决方案1】:

    使用Uri.Builder.appendPath 将主机名和路径添加到 uri 为:

    Uri.Builder builder = new Uri.Builder();
            builder.scheme("https")
            .authority("www.dummyurl.com")
            .appendPath("android")
            .appendPath("agent.php")
            ....
    

    【讨论】:

    • 工作得刚刚好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 2015-01-11
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多