package com.bdyjy.fragment; import android.R.string; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.MediaStore; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.webkit.WebView.FindListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.bdyjy.R; import com.bdyjy.activity.MainActivity; import com.bdyjy.constants.HandlerOrder; import com.bdyjy.entity.News; import com.bdyjy.entity.NewsQueryResultBean; import com.bdyjy.entity.campusguide.CampusGuides; import com.bdyjy.entity.campusguide.CampusGuidesContentQueryResultBean; import com.bdyjy.entity.complaint.ComplaintContentQueryResultBean; import com.bdyjy.entity.complaint.ComplaintQueryResultBean; import com.bdyjy.entity.fix.MyFixContentBean; import com.bdyjy.entity.upload.ImgUploadResultBean; import com.bdyjy.util.OkHttpUtils; import com.bdyjy.util.SPUtils; /** * * Ͷ�߽���fragment * * @author holy 2016.3.28 * */ public class ComplaintFragment extends Fragment { private TextView tv_back; private TextView my_complaint; private Spinner complaint_sppiner; private int complaintChoice; private EditText telephone; private String mTelephone; private EditText describe; private String mDescribe; private Button submit; private String imagePath1 = null; private String imagePath2 = null; private ImageView addimage1; private ImageView addimage2; private Map<String, String> imgsMap = new HashMap<String, String>(); private List<ImgUploadResultBean> uploadResultList = new ArrayList<ImgUploadResultBean>(); private int imgCount = 2; private Handler handler; private String toastMsg; private MainActivity ctx; private void initHandler() { handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case HandlerOrder.TOAST: // TODO Toast.makeText(ctx, toastMsg, Toast.LENGTH_LONG).show(); break; case HandlerOrder.UPDATE_LISTVIEW: // listView1.onLoad(); // loadData(); break; case HandlerOrder.PROCESSBAR_SHOW: ctx.showRoundProcessDialog(); break; case HandlerOrder.PROCESSBAR_HIDE: ctx.hideRoundProcessDialog(); break; case HandlerOrder.UPLOAD_ERROR: Toast.makeText(ctx, "�ϴ������������쳣", Toast.LENGTH_LONG).show(); ctx.hideRoundProcessDialog(); break; case HandlerOrder.UPLOAD_OK: handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE); String result = msg.getData().get("body").toString(); System.out.println("result:\n" + result); ImgUploadResultBean is = JSON.parseObject(result, ImgUploadResultBean.class); if ("0".equals(is.getApp_result_key())) { System.out.println(is.getAttachmentName());// �ļ��� System.out.println(is.getAttachmentUrl());// �ڷ������ϵĴ洢·�� System.out.println(is.getPrix());// �ļ���������ַ System.out.println(is.getType());// �ļ���� System.out.println(is.getSize());// �ļ���С uploadResultList.add(is); } else { Toast.makeText(ctx, "�ϴ�����App_result_key��=0", Toast.LENGTH_LONG).show(); } if (uploadResultList.size() == imgsMap.size())// �������ͼƬ���Ѿ��ϴ���� { ComplaintCommit(); } break; case HandlerOrder.POST_OK: // ���������post֮��Ľ�� handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE); // ����ֵ������JSON��ʽ�����ݣ���Ҫ��������� String res = msg.getData().get("body").toString(); System.out.println("post_result:\n" + res); ComplaintContentQueryResultBean bean = JSON.parseObject( res, ComplaintContentQueryResultBean.class); String app_result_key = bean.getApp_result_key(); if ("0".equals(app_result_key)) { toastMsg = "��Ϣ�ύ�ɹ�"; handler.sendEmptyMessage(HandlerOrder.TOAST); handler.sendEmptyMessage(HandlerOrder.TO_MAIN); ctx.jumpToMyComplaintListFregment(); } else { toastMsg = bean.getApp_result_message_key();// "��Ϣ�ύʧ�ܣ�������"; handler.sendEmptyMessage(HandlerOrder.TOAST); } break; } } }; } public ComplaintFragment(MainActivity ctx) { this.ctx = ctx; initHandler(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.complaint_fragment, null); // ������ҳ tv_back = (TextView) view.findViewById(R.id.tv_back); tv_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ctx.jumpToFirstPageFregment(); } }); // ����Ͷ�߽����б�ҳ my_complaint = (TextView) view.findViewById(R.id.my_compliant); view.findViewById(R.id.ll_my_compliant).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // �����б�ǰ��Ҫˢ��list // getNewsList(); ctx.jumpToMyComplaintListFregment(); } }); // ����ѡ�� complaint_sppiner = (Spinner) view.findViewById(R.id.complaint_spinner); complaint_sppiner .setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub // ��ȡ��Ͷ����� complaintChoice = (int) complaint_sppiner .getSelectedItemId() + 1; // complaint_sppiner.getSelectedItem().toString(); // System.out.println("complaintChoice"+complaintChoice); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); // ��ϵ��ʽ telephone = (EditText) view.findViewById(R.id.telephone); // ���� describe = (EditText) view.findViewById(R.id.describe); // ��ȡͼƬ addimage1 = (ImageView) view.findViewById(R.id.add1); addimage2 = (ImageView) view.findViewById(R.id.add2); addimage2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, 200); } }); // ���ȷ�� submit = (Button) view.findViewById(R.id.submit); submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mTelephone = telephone.getText().toString(); mDescribe = describe.getText().toString(); // ����������������(��ͼƬ) // uploadResultList.clear(); // ComplaintCommit(); // ������ύ��ť��ʱ���ȼ���Ƿ��Ѿ�ѡ����ͼƬ if (!TextUtils.isEmpty(imgsMap.get(img1Key)) || !TextUtils.isEmpty(imgsMap.get(img2Key))) { uploadResultList.clear(); // ���ѡ����ͼƬ try { // TODO ������ύ�������ϴ�ͼƬ��ѭ�������ϴ��ķ��� for (String key : imgsMap.keySet()) { handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW); OkHttpUtils.getInstance().uploadImg(ctx, imgsMap.get(key), handler); handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE); } } catch (Exception e) { e.printStackTrace(); } } else { // ���û��ѡ��ͼƬ��ֱ���ύ ComplaintCommit(); } // updateContent(complaintChoice, mTelephone, mDescribe); // �����ҵ�Ͷ���б�ҳ // ctx.jumpToMyComplaintListFregment(); } }); return view; } private int picIndex = 1; private String img1Key = "img1"; private String img2Key = "img2"; @SuppressLint("DefaultLocale") @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 200 && resultCode == Activity.RESULT_OK) { Log.d("dd", data.toString()); Uri uri = data.getData(); // ���صĽ�� ContentResolver cr = ctx.getContentResolver(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; // /////////////////////////////////////////////////////////////// Cursor cursor = ctx.getContentResolver().query(uri, filePathColumn, null, null, null); if (cursor == null) { System.out.print("cursor :" + null); return; } System.out.println("cursor :" + cursor); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); if (!picturePath.toLowerCase().endsWith(".jpg") && !picturePath.toLowerCase().endsWith(".jpeg")) { Toast.makeText(ctx.getApplicationContext(), "��ѡ��jpg��ʽ��ͼƬ", Toast.LENGTH_SHORT).show(); return; } try { Toast.makeText(ctx, picturePath, Toast.LENGTH_SHORT).show(); if (picIndex == 1) { // �����ォͼ1�ľ���·�������ȥ imgsMap.put(img1Key, picturePath); imagePath1 = picturePath; ShowImg(imagePath1, addimage1); picIndex = 2; } else if (picIndex == 2) { // �����ォͼ1�ľ���·�������ȥ imgsMap.put(img2Key, picturePath); imagePath2 = picturePath; ShowImg(imagePath2, addimage2); picIndex = 1; } // if (imagePath1==null) { // imagePath1=picturePath; // ShowImg(imagePath1, addimage1); // }else{ // imagePath2=picturePath; // ShowImg(imagePath1, addimage1); // ShowImg(imagePath2, addimage2); // } // } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // images.add(picturePath);// ������Ϊ�˻�ȡͼƬ��·�� // setAdapter(new MyAdapter(UploadActivity.this)); Log.d("dd", picturePath); } } public void ShowImg(String uri, ImageView headimage2) throws IOException { FileInputStream fs = new FileInputStream(uri); BufferedInputStream bs = new BufferedInputStream(fs); Bitmap btp = BitmapFactory.decodeStream(bs); headimage2.setImageBitmap(btp); bs.close(); fs.close(); btp = null; } private void ComplaintCommit() { new Thread() { @Override public void run() { // ȡ��ҳ���ϵIJ����Լ�Ӧ���Ѿ��洢�IJ��� String res = null; // ��sharePreference��ȡ��֮ǰ�洢�IJ��� String token = (String) SPUtils.get(ctx, "token", ""); String singnature = (String) SPUtils.get(ctx, "singnature", ""); String st = (String) SPUtils.get(ctx, "st", ""); // ����post���� tryPost(complaintChoice, mTelephone, mDescribe, token, singnature, st); System.out.println("�ϴ������ǣ�" + complaintChoice + mTelephone + mDescribe); } }.start(); } /** * �����첽post��ʽ�����ύ */ private void tryPost(int complaintChoice, String mTelephone, String mDescribe, String token, String singnature, String st) { // ����ʹ���첽post������� HashMap<String, String> map = new HashMap<String, String>(); // ���ڿ�ʼ������� String Choice = String.valueOf(complaintChoice); map.put("title", mDescribe); map.put("type", Choice); map.put("contacts", mTelephone); map.put("content", mDescribe); for (int i = 0; i < uploadResultList.size(); i++) { map.put("attArry[" + i + "].filePath", uploadResultList.get(i) .getAttachmentUrl()); map.put("attArry[" + i + "].fileName", uploadResultList.get(i) .getAttachmentName()); map.put("attArry[" + i + "].fileSize", uploadResultList.get(i) .getSize()); map.put("attArry[" + i + "].fileType", uploadResultList.get(i) .getType()); } String http = "/admin/proposal/addSave.do?token=" + token + "&singnature=" + singnature + "&st=" + st; handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW); OkHttpUtils.getInstance().doPostAsync(ctx, http, map, handler); } }