我想从网址获取源代码。但是我找到的所有东西都被弃用了,或者它不是我想要的东西,所以我仍在寻找,但我一无所获。
弃用:
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
is.close(); // Close the stream
你能帮助我吗?
答案 0 :(得分:1)
使用备用http客户端它会更快更好。我可以推荐你。
答案 1 :(得分:0)
尝试使用apache http
中的HttpClient class Task extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
HttpPost httppost = new HttpPost("https://www.google.com");
httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
String str = EntityUtils.toString(resEntity);
return str;
} catch (Exception ex) {
return ex.getMessage();
}
}
Listener listen;
@Override
protected void onPostExecute(String result) {
listen.returnVal(result);
}
}
public interface Listener {
public void returnVal(String str);
}
public class Activity1 extends ActionBarActivity implements Listener {
...
protected void onCreate(Bundle savedInstanceState) {
....
Task t = new Task();
t.listen = this;
t.execute();
}
@Override
public void returnVal(String str) {
// Http Response
}
不要忘记在你的男人身上添加<uses-permission android:name="android.permission.INTERNET"/>