I. Introduction
Yesterday I learned the simple use of animation in Android.
Today, the UI interface has basically taken shape, and the focus has been on the writing back end, in order to reduce the burden of teammates. After completing the release function, the difficulty encountered was that when the client transmitted data to the server, garbled characters appeared in Chinese. I learned that Tomcat's default encoding format is ISO-8859-1. I checked the information to learn the method of transcoding. Learn better conversion methods in class.
Tomorrow's task is to learn how to use the Android Sqlite database or SharedPreferences, so as to realize the function of remembering the user's login status and continue to beautify the page.
2. Achievement display
Third, the code
SendActivity.java
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 = "spit" ; break ; case R.id.rb_love: type == "confession" ; break ; case R.id.rb_friends: type = "friend" ; break ; case R.id.rb_other: type = "other" ; break ; case R.id.send_fab: PutData (); break ; } } / * Store the data in a hash table * / private void PutData () { stringHashMap = new HashMap <> (); // Make a non-empty judgment on what the user fills if (et_title.getText (). ToString (). Equals ( "" )) { Toast.makeText (SendActivity. This , "Title cannot be empty" , Toast.LENGTH_LONG) .show (); } else if (et_content.getText (). ToString (). Equals ("" )) { Toast.makeText (SendActivity. This , "Content cannot be empty" , Toast.LENGTH_LONG) .show (); } else if (type.equals ("" )) { Toast.makeText (SendActivity. voidthis , "Please select the category you want to publish" , 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 requests a thread * / private void getPosts () { new Thread ( new Runnable () { @Override public 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); // The requested parameters are converted to byte array // byte [] postData = params.getBytes () ; // Create a new URL object URL url = new URL (baseUrl); // Open a HttpURLConnection connection HttpURLConnection urlConn =(HttpURLConnection) url.openConnection (); // Set the connection timeout time urlConn.setConnectTimeout (5 * 1000 ); // Set the timeout for reading data from the host urlConn.setReadTimeout (5 * 1000 ); // Post requests must be set to allow output Default false urlConn.setDoOutput ( true ); // Set request allow input default is true urlConn.setDoInput ( true ); // Post request cannot use cache urlConn.setUseCaches ( false ); // Set to Post request urlConn.setRequestMethod (" POST " ); // Set whether to automatically handle redirection for this connection urlConn.setInstanceFollowRedirects ( true ); // Configuration request Content-Type // urlConn.setRequestProperty ("Content-Type", "application / json"); // Post request cannot be set This // Start connection urlConn.connect (); // Send request parameters PrintWriter dos = new PrintWriter (urlConn.getOutputStream ()); dos.write (params); dos.flush (); dos.close (); // Determine if the request is successful if (urlConn.getResponseCode () == 200 ) { //Get the returned data String result = streamToString (urlConn.getInputStream ()); Looper.prepare (); Toast.makeText (SendActivity. This , result.trim (), Toast.LENGTH_LONG) .show (); Looper.loop () ; Log.e (TAG, "Post request succeeded, result --->" + result); System.out.println (result); } else { Log.e (TAG, "Post request failed" ); } // Close the connection urlConn.disconnect (); } catch (Exception e) { Log.e (TAG, e.toString ()); } } / ** * Convert the input stream to a string * * @param is the input stream obtained from the network * @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
<?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>
Fourth, today's team blog