简单了解Http协议:就是客户端向服务器发送请求,服务器收到请求后返回一些数据,客户端进行解析和处理,就得到了我们想要的效果了。
response和request的区别?
- Request 和 Response 对象起到了服务器与客户机之间的信息传递作用。
- Request:是客户端向服务器发送请求
- Response:是服务器向客户端响应
我们用android中的按钮向服务器发送请求,Webview后台来发送、接收、解析、服务器响应的数据。
我们的布局文件里只用了一个Button(点击按钮)和ScrollView(滑动视图)中包括ViewText(显示数据)
layout布局代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/send_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送请求"></Button>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/response_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView>
</ScrollView>
</LinearLayout>
然后我们在MainActivity中编写逻辑代码
package com.example.networktest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView responseTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendRequest = findViewById(R.id.send_request);
responseTest=(TextView)findViewById(R.id.response_text);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.send_request){
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
URL url = null;
BufferedReader reader = null;
try {
url = new URL("https://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
InputStream in = connection.getInputStream();
//下面对获取到的输入流进行读取
reader = new BufferedReader(new InputStreamReader(in));
StringBuffer response = new StringBuffer();
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){
runOnUiThread(new Runnable() {
@Override
public void run() {
//将服务器响应的数据显示在视图中
responseTest.setText(response);
}
});
}
}
最后附上运行结果