diff --git a/CommentActivity.java b/CommentActivity.java new file mode 100644 index 0000000..0f65080 --- /dev/null +++ b/CommentActivity.java @@ -0,0 +1,252 @@ +package com.example.share2; + +import androidx.annotation.RequiresApi; +import androidx.appcompat.app.AppCompatActivity; + +import android.content.SharedPreferences; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; +import android.util.Log; +import android.view.View; +import android.view.inputmethod.InputMethodManager; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ListView; +import android.widget.RelativeLayout; +import android.widget.TextView; +import android.widget.Toast; +import android.content.Context; +import com.bumptech.glide.Glide; +import com.example.share2.fragment.comment.CommentAdapter; +import com.example.share2.fragment.comment.CommentData; +import com.example.share2.fragment.photo.PhotoData; +import com.example.share2.fragment.photo.Record; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.Headers; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class CommentActivity extends AppCompatActivity { + + private Record record; + private static List commentDataList; + private static ListView listView; + private static ImageView imageViewComm; + private static TextView hide_down ; + private static EditText comEdit; + private static Button bsend; + private static CommentAdapter commentAdapter; + private LinearLayout rl_enroll; + private RelativeLayout rl_comment; + SharedPreferences sp; + + private static final Handler mHandler = new Handler(Looper.getMainLooper()){ + public void handleMessage(Message msg){ + ResponseBody>> photoDataResponseBody; + switch (msg.what) { + case 1: + photoDataResponseBody = (ResponseBody>>) msg.obj; + PhotoData> commentData = photoDataResponseBody.getData(); + if (commentData == null)//防止崩溃 + { + Log.d("失败哦","ssss"); + break; + } + List l; + l = commentData.getRecords(); + + for(CommentData commentData1:l) + commentAdapter.add(commentData1); + commentAdapter.notifyDataSetChanged(); + break; + default: + break; + } + } + }; + + + + + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_comment); + this.getSupportActionBar().hide(); + + sp = getSharedPreferences("config", Context.MODE_PRIVATE); + + + record = (Record) getIntent().getSerializableExtra("record"); + commentDataList = new ArrayList(); + CommentData c = new CommentData(); + c.setAuthor(record.getUsername()); + c.setTitle(record.getTitle()); + c.setUrl(record.getImageUrlList().get(0)); + commentDataList.add(c); + commentAdapter = new CommentAdapter(this,R.layout.comment_item,commentDataList); + listView = findViewById(R.id.lv_comment_list); + listView.setAdapter(commentAdapter); + getData(); + + hide_down = findViewById(R.id.hide_down); + comEdit = findViewById(R.id.comment_content); + bsend = findViewById(R.id.comment_send); + rl_enroll = (LinearLayout) findViewById(R.id.rl_enroll); + rl_comment = (RelativeLayout) findViewById(R.id.rl_comment); + + imageViewComm = findViewById(R.id.commentgive); + imageViewComm.setOnClickListener(new View.OnClickListener() { + @RequiresApi(api = Build.VERSION_CODES.M) + @Override + public void onClick(View view) { + InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); + imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); + rl_enroll.setVisibility(View.GONE); + rl_comment.setVisibility(View.VISIBLE); + } + }); + hide_down.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + rl_enroll.setVisibility(View.VISIBLE); + rl_comment.setVisibility(View.GONE); + InputMethodManager im = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); + im.hideSoftInputFromWindow(comEdit.getWindowToken(), 0); + } + }); + bsend.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if(comEdit.getText().toString().equals("")){ + Toast.makeText(CommentActivity.this, "评论不能为空哦!", Toast.LENGTH_SHORT).show(); + } + else { + CommentData c = new CommentData(); + c.setContent(comEdit.getText().toString()); + String temp = sp.getString("username",""); + c.setUserName(temp); + commentAdapter.add(c); + commentAdapter.notifyDataSetChanged(); + pushData(comEdit.getText().toString()); + InputMethodManager im = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); + im.hideSoftInputFromWindow(comEdit.getWindowToken(), 0); + + } + } + }); + + } + + + + + private void getData(){ + ConnectServer connectServer = new ConnectServer(); + Map addHeader = new HashMap<>(); + addHeader.put("Content-Type","application/json"); + Headers headers = connectServer.getHeaders(addHeader); + + + HashMap bodyParams = new HashMap<>(); + + bodyParams.put("shareId",record.getId()); + + String url = Constants.SERVER_URL+Constants.USER_COMMENT_FIRST+connectServer.getBodyParams(bodyParams); + + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(url) + .headers(headers).get().build(); + + Call call = client.newCall(request); + call.enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + Log.d("获取一级评论失败","sssss"); + } + + @Override + public void onResponse(Call call, Response response) throws IOException { + final String Rbody = response.body().string(); + Gson gson = new Gson(); + Type jsonType = new TypeToken>>>(){}.getType(); + ResponseBody>> dataResponseBody = gson.fromJson(Rbody,jsonType); + Message message = new Message(); + message.what = 1; + message.obj = dataResponseBody; + mHandler.sendMessage(message); + Log.d("获取一级评论成功","sssss"); + } + }); + } + + private void pushData(String commtent){ + ConnectServer connectServer = new ConnectServer(); + Map addHeader = new HashMap<>(); + addHeader.put("Content-Type","application/json"); + Headers headers = connectServer.getHeaders(addHeader); + + Map bodyParams = new HashMap<>(); + bodyParams.put("shareId",record.getId()); + String userId = sp.getString("id",""); + String userName = sp.getString("username",""); + bodyParams.put("userId",userId); + bodyParams.put("userName",userName); + bodyParams.put("content",commtent); + RequestBody r_body = connectServer.getJsonBody(bodyParams); + String url = Constants.SERVER_URL+Constants.USER_COMMENT_FIRST; + + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(url) + .headers(headers).post(r_body).build(); + + Call call = client.newCall(request); + call.enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + Log.d("获取一级评论失败","sssss"); + } + + @Override + public void onResponse(Call call, Response response) throws IOException { + final String Rbody = response.body().string(); + Gson gson = new Gson(); +// Type jsonType = new TypeToken>>>(){}.getType(); +// ResponseBody>> dataResponseBody = gson.fromJson(Rbody,jsonType); +// Message message = new Message(); +// message.what = 1; +// message.obj = dataResponseBody; +// mHandler.sendMessage(message); +// Log.d("获取一级评论成功","sssss"); + } + }); + } + + + + + + +} \ No newline at end of file