我正在尝试使用 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/