通过前面的努力,我们终于完成了三大模块(我的、发现、查找)的内容了,就还剩发布页了,所以接下来就开始处理发布功能
- 首先还是和每个模块处理一样,先在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(); } } } }
——执行效果图: