HttpURLConnect类位于Java.net包中,用于发送HTTP请求和获取HTTP响应。
1.发送get请求
(1)main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="88dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="350dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
2.创建成员变量,在MainAtivity中
private EditText content;//输入文本对象
private Button button;//发表的按钮
private Handler handler;//声明一个Handler对象
private String result="";//显示内容的字符串
private TextView resultTV;//显示结果的文本框对象
3.创建send方法,用于建立一个HTTP连接,并将输入的内容发送到web服务器上,在读取服务器的处理结果
public void send(){
String target="";
target="http://192.168.1.105:8080/blog/index.jsp?content="+base64(content.getText().toString().trim());//要访问的地址
URL url;
try {
url=new URL(target);//创建URL对象
HttpURLConnection connection=(HttpURLConnection)url.openConnection();//创建一个HTTP连接
InputStreamReader in=new InputStreamReader(connection.getInputStream());//获得读取的内容
BufferedReader bufferedReader=new BufferedReader(in);//获取输入流对象
String inputLine=null;
//通过循环逐行读取输入流中的内容
while((inputLine=bufferedReader.readLine())!=null){
result+=inputLine+"\n";
}
in.close();//关闭字符输入流对象
connection.disconnect();//断开连接
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
4.处理乱码的方法
private String base64(String trim) {
try {
trim= Base64.encodeToString(trim.getBytes("utf-8"),Base64.DEFAULT);
trim= URLEncoder.encode(trim);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return trim;
}
5.Oncreate 方法中实现调用send方法来创建连接, 使到的线程,用于发送并读取微博信息
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content=(EditText)findViewById(R.id.editText);
resultTV=(TextView)findViewById(R.id.result);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if("".equals(content.getText().toString())){
Toast.makeText(MainActivity.this,"请输入要输入的内容",Toast.LENGTH_LONG).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
send();
Message m=handler.obtainMessage();
handler.sendMessage(m);
}
}).start();
}
});
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(result!=null){
resultTV.setText(result);
content.setText("");
}
super.handleMessage(msg);
}
};
}
}
7.AndroidManifest.xml运行访问网络资源的权限
在Application 外定义
ses-permission android:name="android.permission.INTERNET"/>
在Application 内定义允许Http访问
android:usesCleartextTraffic="true"
8.index.jsp内容
位于Tomcat\webapps\blog目录下
<%@page contentType="text/html;charset=utf-8" language="java" import="sun.misc.BASE64Decoder"%>
<%
String content="";
if(request.getParameter("content")!=null){
content=request.getParameter("content");
content=content.replaceAll("%2B","+");
BASE64Decoder decoder=new BASE64Decoder();
content=new String(decoder.decodeBuffer(content),"utf-8");
}
%>
<%="发表一条微博,内容如下:"%>
<%=content%>
9.结果如下