android - 从 android 调用外部 URL 并获得响应

标签 android url call

我正在尝试使用 android 调用 URL

HttpClient mClient= new DefaultHttpClient()

HttpGet get = new HttpGet("www.google.com ");

mClient.execute(get);

HttpResponse res = mClient.execute(get);

但是,我没有得到任何回应。如何在 Android 中调用 URL?

最佳答案

这是一个完整的例子:

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(yourURL);
        HttpResponse response = httpclient.execute(httpget);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {                    
            sb.append(line + NL);
        }
        in.close();
        String result = sb.toString();
        Log.v("My Response :: ", result);

使用协议(protocol)为“https://”的 url

"https://www.stackoverflow.com" instead of just "www.stackoverflow.com"

请务必在您的 androidmanifest.xml 中拥有此权限

<uses-permission
    android:name="android.permission.INTERNET" />

关于android - 从 android 调用外部 URL 并获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4889618/

相关文章:

Android Sdk 管理器无法下载任何源列表

android - 如何根据 Android 中对服务器的异步后台调用结果返回 fragment 的 onCreateView 方法内的两个不同 View ?

iphone - 在提交到应用商店之前,是否可以找到 iOS 应用程序的 itunes 链接?

html - 在 VueJS 中访问查询字符串

java - 以编程方式接听电话

android - ConstraintLayout 中的 CardView 未环绕高度

java - Android JDOM2 xml解析,IOExcetion

javascript - PHP - 用户链接

c++ - 在命名空间中调用函数

function - 使用多个参数从.ps1运行PowerShell的函数