通過前面的努力,我們終於完成了三大模塊(我的、發現、查找)的內容了,就還剩發佈頁了,所以接下來就開始處理髮布功能
- 首先還是和每個模塊處理一樣,先在MyEclipse下面的 myServlet.data包中創建servlet命名爲AddInfo.java,這裏主要是發佈內容並返回是否發佈成功
——AddInfo.java文件的完整代碼如下
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.utils.JdbcUtils;
/**
* Servlet implementation class AddInfo
*/
@WebServlet("/AddInfo")
public class AddInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddInfo() {
super();
// TODO Auto-generated constructor stub
}/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}//處理中文字符串
public String handleString(String s) {
try {
byte bb[]=s.getBytes("ISO8859-1");
s=new String(bb,"UTF-8");
}catch(Exception e){}
return s;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
Integer infoId =0; //內容ID,默認設置爲0
String infoTitle = request.getParameter("infoTitle"); //內容標題
String infoDescribe =request.getParameter("infoDescribe"); //內容簡述
String infoDetail = request.getParameter("infoDetail"); //內容詳情String typeStr = request.getParameter("type"); //類型
Integer type = 0;
if(typeStr == null||typeStr == "") {
typeStr = "0";
}
try {
type = Integer.parseInt(typeStr); //內容類型
} catch (NumberFormatException e) {
e.printStackTrace();
}
Integer support = 0; //點贊數,默認設置爲0
String infoAuthor = request.getParameter("infoAuthor"); //作者
Connection connection = null;
PreparedStatement prepareStatement = null;
try {
connection = JdbcUtils.getConnection();
//3.獲取statement
//String sql ="insert into user(name,phone,password,gender,province,city,create_time,update_time) value(?,?,?,?,?,?,?,?) ";
String sql ="INSERT INTO info VALUES(?,?,?,?,?,?,?)";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setInt(1, infoId); //自增
prepareStatement.setString(2, infoTitle);
prepareStatement.setString(3, infoDescribe);
prepareStatement.setString(4, infoDetail);
prepareStatement.setInt(5, type);
prepareStatement.setInt(6, support);
prepareStatement.setString(7, infoAuthor);
//4.執行sql
prepareStatement.execute();
out.print("發佈成功!");
} catch (Exception e) {
e.printStackTrace();
out.print("發佈失敗!"+e);
}finally {
//5.釋放資源 connection prepareStatement
JdbcUtils.close(connection, prepareStatement, null);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}}
2. 在Android項目的layout下找到之前創建好的edit_tab_content.xml,開始編寫佈局文件
- 對該頁面初始設計是希望可以有四個切換分享內容的按鈕,分別對應“分享趣事”“分享圖片”“分享音樂”“發佈日記”
- 在按鈕下是內容編輯框,包括標題、簡述、詳情以及底部的一個發佈按鈕
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:gravity="center_vertical" android:orientation="horizontal" android:id="@+id/title_bar" android:background="@drawable/title_bg_night" android:layout_width="fill_parent" android:layout_height="35.0dip"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/app_icon" /> <LinearLayout android:gravity="center_vertical" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_weight="1.0"> <TextView android:textSize="16.0sp" android:textColor="@color/titleTextColor" android:singleLine="true" android:id="@+id/myTitle" android:layout_width="fill_parent" android:ellipsize="end" android:layout_height="wrap_content" android:layout_marginLeft="2.0dip" android:text="@string/app_name" /> </LinearLayout> <ImageButton android:id="@+id/back_button" android:layout_gravity="center" android:background="@color/transparent" android:layout_width="60.0dip" android:layout_height="29.0dip" android:layout_marginRight="5.0dip" android:src="@drawable/exit" /> </LinearLayout> <!-- <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/tab_icon_four" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="10dp" android:text="這是發佈界面" android:textSize="20sp" /> </LinearLayout>--> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="50dp"> <RelativeLayout android:id="@+id/bt_share_funny" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/share_funny_image_bc" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:src="@drawable/share_funny_select"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="分享趣事" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/bt_share_pic" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/share_pic_image_bc" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:src="@drawable/share_pic_no_select" /> <TextView android:id="@+id/find_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="分享圖片" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/bt_share_music" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/share_music_image_bc" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:src="@drawable/share_music_no_select" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="分享音樂" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/bt_share_diary" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <ImageView android:id="@+id/share_diary_image_bc" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:src="@drawable/share_diary_no_select" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="發佈日記" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> </LinearLayout> <!--分享趣--> <LinearLayout android:id="@+id/edit_base_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@+id/rl_login_top" android:layout_marginTop="140dp" android:layout_marginLeft="26dp" android:layout_marginRight="26dp" android:gravity="center" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="標 題:"/> <EditText android:id="@+id/et_edit_title_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLength="10" android:paddingLeft="10.0dip" android:singleLine="true" android:hint="標題請保持在10個字以內"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLength="40" android:text="簡 述:"/> <EditText android:id="@+id/et_edit_describe_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10.0dip" android:hint="簡述你要分享的趣事" /> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="分 享 詳 情:"/> <LinearLayout android:id="@+id/edit_detail_info_layout" android:layout_width="match_parent" android:layout_height="250dp" android:padding="10.0dip" android:background="@drawable/edit_layout_border" android:orientation="horizontal"> <EditText android:id="@+id/et_edit_detail_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10.0dip" android:background="@null" android:hint="在這詳細介紹趣事"/> </LinearLayout> <Button android:id="@+id/edit_sure_share" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="100dp" android:layout_marginRight="100dp" android:background="@drawable/selector_loginactivity_button" android:text="發佈" android:textColor="#fff" android:gravity="center" /> </LinearLayout> </RelativeLayout>
3.找到之前就創建好的EditTabFragment.java文件,在這裏主要需處理的就是按鈕的事件和發佈時請求後臺
- 頂部四個分享內容按鈕切換時改變編輯框的內容提示以及發佈按鈕的文字
- 發佈成功之後清空編輯框的內容
——EditTabFragment.java文件完整代碼如下
package com.example.discover; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.os.StrictMode; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class EditTabFragment extends Fragment { RelativeLayout bt_share_funny,bt_share_pic,bt_share_music,bt_share_diary; //四種類型的內容分享 LinearLayout edit_detail_info_layout; EditText et_edit_title_info,et_edit_describe_info; //標題和簡述 EditText et_edit_detail_info; //分享的詳情 Button edit_sure_share; //發佈按鈕_ ImageView share_funny_image_bc,share_pic_image_bc,share_music_image_bc,share_diary_image_bc; String baseUrl = "http://10.0.2.2:8080/iShareService/servlet/"; //web服務器的地址 String imgBaseUrl = "http://10.0.2.2:8080/iShareService/images/"; //圖片資源 Integer infoType = 0; //默認是分享趣事 Activity mActivity; //存放當前的activity public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View moreLayout = inflater.inflate(R.layout.edit_tab_content, container, false); return moreLayout; } //APP如果在主線程中請求網絡操作,將會拋出異常,所以需要用線程來操作網絡請求 void setVersion() { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() //探測SQLite數據庫操作 .penaltyLog() //打印logcat .penaltyDeath() .build()); } //綁定點擊事件 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mActivity = this.getActivity(); bt_share_funny = (RelativeLayout) getActivity().findViewById(R.id.bt_share_funny); //分享趣事 bt_share_pic = (RelativeLayout) getActivity().findViewById(R.id.bt_share_pic); //分享圖片 bt_share_music = (RelativeLayout) getActivity().findViewById(R.id.bt_share_music); //分享音樂 bt_share_diary = (RelativeLayout) getActivity().findViewById(R.id.bt_share_diary); //發佈日記 edit_detail_info_layout = (LinearLayout) getActivity().findViewById(R.id.edit_detail_info_layout);// 詳情框 et_edit_title_info = (EditText) getActivity().findViewById(R.id.et_edit_title_info); //分享標題 et_edit_describe_info = (EditText) getActivity().findViewById(R.id.et_edit_describe_info); //分享簡述 et_edit_detail_info = (EditText) getActivity().findViewById(R.id.et_edit_detail_info); //分享簡述 edit_sure_share = (Button) getActivity().findViewById(R.id.edit_sure_share); //確定分享 share_funny_image_bc = (ImageView) getActivity().findViewById(R.id.share_funny_image_bc); share_pic_image_bc = (ImageView) getActivity().findViewById(R.id.share_pic_image_bc); share_music_image_bc = (ImageView) getActivity().findViewById(R.id.share_music_image_bc); share_diary_image_bc = (ImageView) getActivity().findViewById(R.id.share_diary_image_bc); //默認顯示分享趣事 changeEditDisplay(infoType); bt_share_funny.setOnClickListener(new btnClick()); bt_share_pic.setOnClickListener(new btnClick()); bt_share_music.setOnClickListener(new btnClick()); bt_share_diary.setOnClickListener(new btnClick()); edit_detail_info_layout.setOnClickListener(new btnClick()); edit_sure_share.setOnClickListener(new okClick()); } //改變UI public void changeEditDisplay(int selectType) { if(selectType==0){ setAllBc(); setAllNone(); share_funny_image_bc.setImageResource(R.drawable.share_funny_select); et_edit_title_info.setHint("在這裏填寫標題"); et_edit_describe_info.setHint("簡述你要分享的趣事"); et_edit_detail_info.setHint("在這詳細介紹趣事"); edit_sure_share.setText("分享趣事"); }else if(selectType==1){ setAllBc(); setAllNone(); share_pic_image_bc.setImageResource(R.drawable.share_pic_select); et_edit_title_info.setHint("在這裏填寫標題"); et_edit_describe_info.setHint("在這裏填寫圖片鏈接"); et_edit_detail_info.setHint("在這裏填寫圖片詳細介紹"); edit_sure_share.setText("分享圖片"); }else if(selectType==2){ setAllBc(); setAllNone(); share_music_image_bc.setImageResource(R.drawable.share_music_select); et_edit_title_info.setHint("在這裏填寫歌名"); et_edit_describe_info.setHint("在這裏填寫音樂鏈接"); et_edit_detail_info.setHint("在這裏填寫關於音樂的感受"); edit_sure_share.setText("分享音樂"); }else if(selectType==3){ setAllBc(); setAllNone(); share_diary_image_bc.setImageResource(R.drawable.share_diary_select); et_edit_title_info.setHint("在這裏填寫標題"); et_edit_describe_info.setHint("在這裏填寫今天的心情"); et_edit_detail_info.setHint("在這裏記錄今天的事"); edit_sure_share.setText("發佈日記"); } } //讓所有的按鈕變成灰色 public void setAllBc(){ share_funny_image_bc.setImageResource(R.drawable.share_funny_no_select); share_pic_image_bc.setImageResource(R.drawable.share_pic_no_select); share_music_image_bc.setImageResource(R.drawable.share_music_no_select); share_diary_image_bc.setImageResource(R.drawable.share_diary_no_select); } //清空所有文本輸入框的值 public void setAllNone(){ et_edit_title_info.setText(""); et_edit_describe_info.setText(""); et_edit_detail_info.setText(""); } //點擊不同分享內容的時候改變UI class btnClick implements View.OnClickListener { public void onClick(View v) { if (v == bt_share_funny) { infoType = 0; changeEditDisplay(infoType); /*Toast.makeText(getActivity(), textView.getText(), Toast.LENGTH_LONG).show();*/ }else if(v == bt_share_pic){ infoType = 1; changeEditDisplay(infoType); }else if(v == bt_share_music){ infoType = 2; changeEditDisplay(infoType); }else if(v == bt_share_diary){ infoType = 3; changeEditDisplay(infoType); }else if(v == edit_detail_info_layout){ et_edit_detail_info.setFocusable(true); et_edit_detail_info.setFocusableInTouchMode(true); Toast.makeText(getActivity(), "點擊了!", Toast.LENGTH_LONG).show(); } } } //點擊不同分享內容的時候改變UI class okClick implements View.OnClickListener { public void onClick(View v) { SharedPreferences sp = mActivity.getSharedPreferences("save", Context.MODE_PRIVATE); Boolean isLogin = sp.getBoolean("isLogin",false); if(isLogin){ setVersion(); String userName = sp.getString("name",""); String title = et_edit_title_info.getText().toString(); String describe = et_edit_describe_info.getText().toString(); String detail = et_edit_detail_info.getText().toString(); Boolean boo = title.length()>0&&describe.length()>0&&detail.length()>0; if(boo){ StringBuilder stringBuilder = new StringBuilder(); BufferedReader buffer = null; HttpURLConnection connGET = null; try { String registerUrl = baseUrl+"AddInfo?infoTitle="+title+"&infoDescribe="+describe+"&infoDetail="+detail +"&type="+infoType+"&infoAuthor="+userName; URL url = new URL(registerUrl); connGET = (HttpURLConnection) url.openConnection(); connGET.setConnectTimeout(5000); connGET.setRequestMethod("GET"); if (connGET.getResponseCode() == 200) { buffer = new BufferedReader(new InputStreamReader(connGET.getInputStream())); for (String s = buffer.readLine(); s != null; s = buffer.readLine()) { stringBuilder.append(s); } setAllNone(); Toast.makeText(getActivity(), stringBuilder, Toast.LENGTH_LONG).show(); buffer.close(); }else{ Toast.makeText(getActivity(), "非200.."+connGET.getResponseCode(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(getActivity(), "get 提交 err.." + e.toString(), Toast.LENGTH_LONG).show(); } }else{ Toast.makeText(getActivity(), "清將信息填寫完整!", Toast.LENGTH_LONG).show(); } }else{ Toast.makeText(getActivity(), "清先登錄!", Toast.LENGTH_LONG).show(); } } } }
——執行效果圖: