一、前言
昨天学习了Android中动画的简单使用。
今天开始UI界面已基本成型,开始把侧重点放在写后端上,以此来减轻队友的负担。完成了发布的功能,遇到的困难是客户端给服务端传输数据的时候,中文出现了乱码,了解到Tomcat默认编码形式为ISO-8859-1,查阅资料了解到了转码的办法,通过下午上课学到更好的转换方式。
明天的任务学习Android Sqlite数据库或SharedPreferences的使用,从而实现记住用户登录状态的功能,对页面继续进行美化。
二、成果展示
三、代码
SendActivity.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.androidlearing.tdtreehole.activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.util.Log; import android.view.View; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.androidlearing.tdtreehole.R; import com.google.android.material.floatingactionbutton.FloatingActionButton; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; public class SendActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "SendActivity"; private TextView et_title,et_content; private RadioButton rb_complain,rb_love,rb_friends,rb_other; private FloatingActionButton send_fab; private HashMap<String, String> stringHashMap; private String type=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send); initView(); } private void initView() { et_title = findViewById(R.id.et_title); et_content = findViewById(R.id.et_content); rb_complain = findViewById(R.id.rb_complain); rb_friends = findViewById(R.id.rb_friends); rb_love = findViewById(R.id.rb_love); rb_other = findViewById(R.id.rb_other); send_fab = findViewById(R.id.send_fab); rb_complain.setOnClickListener(this); rb_friends.setOnClickListener(this); rb_love.setOnClickListener(this); rb_other.setOnClickListener(this); send_fab.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.rb_complain: type="吐槽"; break; case R.id.rb_love: type="表白"; break; case R.id.rb_friends: type="交友"; break; case R.id.rb_other: type="其他"; break; case R.id.send_fab: PutData(); break; } } /* 将数据用哈希表存储起来 */ private void PutData() { stringHashMap = new HashMap<>(); // 对用户所填内容进行非空判断 if (et_title.getText().toString().equals("")){ Toast.makeText(SendActivity.this, "标题不能为空", Toast.LENGTH_LONG).show(); }else if (et_content.getText().toString().equals("")){ Toast.makeText(SendActivity.this, "内容不能为空", Toast.LENGTH_LONG).show(); }else if (type.equals("")){ Toast.makeText(SendActivity.this, "请选择您要发布的类别", Toast.LENGTH_LONG).show(); }else { stringHashMap.put("title",et_title.getText().toString()); stringHashMap.put("content",et_content.getText().toString()); stringHashMap.put("type",type); getPosts(); } } /* post请求一个线程 */ private void getPosts() { new Thread(new Runnable() { @Override public void run() { requestPost(stringHashMap); } }).start(); } /* post提交数据 */ private void requestPost(HashMap<String, String> paramsMap) { try { String baseUrl = "http://10.0.2.2:8080/TDTreeHole/Post"; //合成参数 StringBuilder tempParams = new StringBuilder(); int pos = 0; for (String key : paramsMap.keySet()) { if (pos >0) { tempParams.append("&"); } tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8"))); pos++; } String params = tempParams.toString(); Log.e(TAG,"params--post-->>"+params); // 请求的参数转换为byte数组 // byte[] postData = params.getBytes(); // 新建一个URL对象 URL url = new URL(baseUrl); // 打开一个HttpURLConnection连接 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); // 设置连接超时时间 urlConn.setConnectTimeout(5 * 1000); //设置从主机读取数据超时 urlConn.setReadTimeout(5 * 1000); // Post请求必须设置允许输出 默认false urlConn.setDoOutput(true); //设置请求允许输入 默认是true urlConn.setDoInput(true); // Post请求不能使用缓存 urlConn.setUseCaches(false); // 设置为Post请求 urlConn.setRequestMethod("POST"); //设置本次连接是否自动处理重定向 urlConn.setInstanceFollowRedirects(true); //配置请求Content-Type // urlConn.setRequestProperty("Content-Type", "application/json");//post请求不能设置这个 // 开始连接 urlConn.connect(); // 发送请求参数 PrintWriter dos = new PrintWriter(urlConn.getOutputStream()); dos.write(params); dos.flush(); dos.close(); // 判断请求是否成功 if (urlConn.getResponseCode() == 200) { // 获取返回的数据 String result = streamToString(urlConn.getInputStream()); Looper.prepare(); Toast.makeText(SendActivity.this, result.trim(), Toast.LENGTH_LONG).show(); Looper.loop(); Log.e(TAG, "Post方式请求成功,result--->" + result); System.out.println(result); } else { Log.e(TAG, "Post方式请求失败"); } // 关闭连接 urlConn.disconnect(); } catch (Exception e) { Log.e(TAG, e.toString()); } } /** * 将输入流转换成字符串 * * @param is 从网络获取的输入流 * @return */ public String streamToString(InputStream is) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } baos.close(); is.close(); byte[] byteArray = baos.toByteArray(); return new String(byteArray); } catch (Exception e) { Log.e(TAG, e.toString()); return null; } } }
activity_send.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg"> <androidx.cardview.widget.CardView android:id="@+id/cv" android:layout_width="300dp" android:layout_height="400dp" android:layout_gravity="center" app:cardCornerRadius="10dp" app:contentPadding="20dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入您要发布的标题:" android:layout_marginTop="5dp"/> <EditText android:id="@+id/et_title" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="10dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="请选择您发布内容的类别:" /> <RadioGroup android:id="@+id/rg_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="65dp" android:gravity="center"> <RadioButton android:id="@+id/rb_complain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吐槽"/> <RadioButton android:id="@+id/rb_love" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="表白"/> <RadioButton android:id="@+id/rb_friends" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="交友"/> <RadioButton android:id="@+id/rb_other" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其他"/> </RadioGroup> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入您要发布的内容:" android:layout_marginTop="95dp"/> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" /> </androidx.cardview.widget.CardView> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/send_fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" app:srcCompat="@android:drawable/ic_input_add" android:layout_marginBottom="50dp"/> </FrameLayout>
四、今日团队博客