一、简介
二、WebView
public class MainActivity extends AppCompatActivity {
private EditText et_url;
private Button btn_join;
private WebView wv_web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_url = (EditText) findViewById(R.id.et_url);
btn_join = (Button) findViewById(R.id.btn_join);
wv_web = (WebView) findViewById(R.id.wv_web);
//支持js
wv_web.getSettings().setJavaScriptEnabled(true);
// 当跳转到新的网页时,仍然在这个WebView中显示
wv_web.setWebViewClient(new WebViewClient());
wv_web.loadUrl("http://www.baidu.com");
btn_join.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String urlStr = et_url.getText().toString();
wv_web.loadUrl(urlStr);
}
});
}
}
PS:感觉比ios的WebView好使
三、使用HTTP协议访问网络
3.1 HttpURLConnection
package com.example.jony.httpdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLConActivity extends AppCompatActivity {
private static final String TAG = "URLConActivity";
private EditText et_url_url;
private Button btn_url_request;
private TextView response_url_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_urlcon);
et_url_url = (EditText) findViewById(R.id.et_url_url);
response_url_text = (TextView) findViewById(R.id.response_url_text);
btn_url_request = (Button) findViewById(R.id.btn_url_request);
btn_url_request.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendRequestWithHttpURLConnection();
}
});
}
private void sendRequestWithHttpURLConnection(){
//子线程中进行网络数据请求
new Thread(new Runnable() {
@Override
public void run() {
//创建连接对象
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(et_url_url.getText().toString());
connection = (HttpURLConnection) url.openConnection();
//get方法
connection.setRequestMethod("GET");
//post方法
// connection.setRequestMethod("POST");
// DataOutputStream out = new DataOutputStream(connection.getOutputStream());
// out.writeBytes("userName=admin&password=123456");
//连接超时和读取超时单位毫秒
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null){
response.append(line);
}
showResponse(response.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null){
connection.disconnect();
}
}
}
}).start();
}
private void showResponse(final String response){
//将线程切换到主线程更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// Log.d(TAG, "run:"+response);
response_url_text.setText(response);
}
});
}
}
3.2 OKHttp
package com.example.jony.httpdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OKHttpActivity extends AppCompatActivity {
private EditText et_ok_url;
private Button btn_ok_request;
private TextView response_ok_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_okhttp);
et_ok_url = (EditText) findViewById(R.id.et_ok_url);
btn_ok_request = (Button) findViewById(R.id.btn_ok_request);
response_ok_text = (TextView) findViewById(R.id.response_ok_text);
btn_ok_request.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendRequestWithOKHttp();
}
});
}
private void sendRequestWithOKHttp(){
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
// //Post请求
// RequestBody requestBody = new FormBody.Builder()
// .add("username","admin")
// .add("password", "123456")
// .build();
// Request request = new Request().Builder()
// .url("http://www.baidu.com")
// .post(requestBody)
// .build();
Request request = new Request.Builder().url(et_ok_url.getText().toString()).build();
try {
//创建call对象,并execute()方法发送请求获取返回数据
Response response = client.newCall(request).execute();
String responseData = response.body().string();
showResponse(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void showResponse(final String response){
runOnUiThread(new Runnable() {
@Override
public void run() {
response_ok_text.setText(response);
}
});
}
}