我正在尝试向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发出请求的方法。我知道我在这里缺少一小部分,但目前无法发现它。

最佳答案

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

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

09-11 04:33
查看更多