/** * ά��������ҳ * created by songdebin * 2016-04-06 * ***/ package com.bdyjy.fragment; 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 org.apache.http.message.BasicNameValuePair; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.content.ContentResolver; import android.content.Context; 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.View.OnClickListener; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import com.alibaba.fastjson.JSON; import com.bdyjy.R; import com.bdyjy.activity.MainActivity; import com.bdyjy.constants.Const; import com.bdyjy.constants.HandlerOrder; import com.bdyjy.entity.fix.MyFixContentBean; import com.bdyjy.entity.fixtype.FixType; import com.bdyjy.entity.fixtype.Type; import com.bdyjy.entity.upload.ImgUploadResultBean; import com.bdyjy.util.HttpXmlClient; import com.bdyjy.util.OkHttpUtils; import com.bdyjy.util.PropetiesFileReaderUtil; import com.bdyjy.util.SPUtils; import com.bdyjy.util.StringUtil; @SuppressLint("ValidFragment") public class AskFixFragment extends Fragment { private MainActivity ctx; private TextView tv_back; private Button btn_my; private Button btn_commit; private ArrayList list;// ��¼��ת��·�� private Spinner sp_type; private String imagePath1 = null; private String imagePath2 = null; private ImageView addimage1; private ImageView addimage2; private RelativeLayout lv_addImage; private EditText et_title; private EditText et_location; private EditText et_tel; private EditText et_description; private Map<String, String> imgsMap = new HashMap<String, String>(); private List<ImgUploadResultBean> uploadResultList = new ArrayList<ImgUploadResultBean>(); private int imgCount = 2; private View mainView; private List<com.bdyjy.entity.fixtype.Type> TypeList; private Map<String, String> TypeMap; public AskFixFragment(ArrayList list, MainActivity ctx) { this.list = list; this.ctx = ctx; } Handler handler = null; String toastMsg = null; private void initHandler() { handler = new Handler(ctx.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case HandlerOrder.TOAST: Toast.makeText(ctx, toastMsg, Toast.LENGTH_LONG).show(); break; case HandlerOrder.TO_MAIN: btn_my.performClick(); 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: 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())// �������ͼƬ���Ѿ��ϴ���� { FixCommit(); } break; case HandlerOrder.UPDATE_LISTVIEW: TypeMap = new HashMap<String, String>(); List<String> typeNameList = new ArrayList<String>(); for (Type t : TypeList) { TypeMap.put(t.getText(), t.getId()); typeNameList.add(t.getText()); } String type_spin[] = typeNameList.toArray(new String[] {}); // ���������˵�����ʽ sp_type = (Spinner) mainView .findViewById(R.id.spinner_askfix); ArrayAdapter<String> ad = new ArrayAdapter<String>(ctx, R.layout.fix_type, type_spin); sp_type.setAdapter(ad); Log.i("spinner", sp_type.getSelectedItem().toString()); 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); MyFixContentBean bean = JSON.parseObject(res, MyFixContentBean.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); } else { toastMsg = bean.getApp_result_message_key();// "��Ϣ�ύʧ�ܣ�������"; handler.sendEmptyMessage(HandlerOrder.TOAST); } break; } } }; } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.askfix_fragment, null); initHandler(); mainView = view; // ������һ���� ������ tv_back = (TextView) view.findViewById(R.id.tv_back); tv_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ctx.backToClickWithId(list); } }); // ��������ؼ��ij�ʼ�� et_title = (EditText) view.findViewById(R.id.et_title_fix_release); et_location = (EditText) view .findViewById(R.id.et_location_fix_release); et_tel = (EditText) view.findViewById(R.id.et_tel_fix_release); et_description = (EditText) view .findViewById(R.id.et_description_fix_release); // ���÷�������������������ʽ getTypes(); btn_commit = (Button) view.findViewById(R.id.btn_fix_release); btn_commit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO ��ʱ�����ύ String title = et_title.getText().toString().trim(); String tel = et_tel.getText().toString().trim(); String location = et_location.getText().toString().trim(); String description = et_description.getText().toString().trim(); if(TextUtils.isEmpty(title)){ toastMsg = "����д����"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else if(TextUtils.isEmpty(tel)){ toastMsg = "����д�����ϵ��ʽ"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else if(TextUtils.isEmpty(location)){ toastMsg = "������ĵ�ַ"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else if(TextUtils.isEmpty(description)){ toastMsg = "����������"; handler.sendEmptyMessage(HandlerOrder.TOAST); }else{ // ������ύ��ť��ʱ���ȼ���Ƿ��Ѿ�ѡ����ͼƬ 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); } } catch (Exception e) { e.printStackTrace(); } } else { // ���û��ѡ��ͼƬ��ֱ���ύ FixCommit(); } } } }); // ��ʼ����������ͼƬ�ĵط� addimage1 = (ImageView) view.findViewById(R.id.iv_show1_fix_release); addimage2 = (ImageView) view.findViewById(R.id.iv_show2_fix_release); // ���ͼƬ lv_addImage = (RelativeLayout) view .findViewById(R.id.lv_addimage_fix_release); lv_addImage.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); } }); // ��ת���ҵ�ά������ btn_my = (Button) view.findViewById(R.id.setting); btn_my.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ctx.jumpToClickWithId(Const.FRAGMENT_MYFIX_ID, list); } }); return view; } /** * * �ύά���������� * */ public void FixCommit() { new Thread() { @Override public void run() { // ȡ��ҳ���ϵIJ����Լ�Ӧ���Ѿ��洢�IJ��� String res = null; String type1 = sp_type.getSelectedItem().toString().trim(); // ��sharePreference��ȡ��֮ǰ�洢�IJ��� String token = (String) SPUtils.get(ctx, "token", ""); String singnature = (String) SPUtils.get(ctx, "singnature", ""); String st = (String) SPUtils.get(ctx, "st", ""); // int type = (type1 == "����") ? 1 : (type1 == "����") ? 2 : 3; String type = TypeMap.get(type1); String title = et_title.getText().toString().trim(); String tel = et_tel.getText().toString().trim(); String location = et_location.getText().toString().trim(); String description = et_description.getText().toString().trim(); // ����post���� tryPost(title, type, tel, location, description, token, singnature, st); // tryPost2(title, type, tel, location, description, token, // singnature, st); // useHttpClient(title, type, tel, location, description, token, // singnature, st); // �ٳ���һ�� �������get���� // String http = createHttpUrlWithEncoding(title, type, tel, // location, description, token, singnature, st); // // // ����δ�����getͬ������ // // String http = createHttpUrl(title, type, tel, location, // // description, // // token, singnature, st); // // try // { // handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW); // res = OkHttpUtils.getInstance().doGet(ctx, http); // handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE); // } catch (Exception e) // { // e.printStackTrace(); // toastMsg = "��Ϣ�ύʧ�ܣ�������"; // handler.sendEmptyMessage(HandlerOrder.TOAST); // return; // } // System.out.println("�ύ����ֵ��" + res.trim()); // // // ����ֵ������JSON��ʽ�����ݣ���Ҫ��������� // MyFixContentBean bean = JSON.parseObject(res, // MyFixContentBean.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); // // } else // { // toastMsg = bean.getApp_result_message_key();// "��Ϣ�ύʧ�ܣ�������"; // handler.sendEmptyMessage(HandlerOrder.TOAST); // } } }.start(); } /** * ��ѯά��������� */ public void getTypes() { new Thread() { @Override public void run() { // ����get���� 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", ""); String http = "/admin/maintenance/initAdd.do?token=" + token + "&singnature=" + singnature + "&st=" + st; System.out.println(http); try { handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW); res = OkHttpUtils.getInstance().doGet(ctx, http); handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE); } catch (Exception e) { e.printStackTrace(); toastMsg = "��ȡʧ��"; handler.sendEmptyMessage(HandlerOrder.TOAST); return; } System.out.println("�ύ����ֵ��" + res.trim()); // ����ֵ������JSON��ʽ�����ݣ���Ҫ��������� FixType bean = JSON.parseObject(res, FixType.class); String app_result_key = bean.getApp_result_key(); if ("0".equals(app_result_key)) { // TypeList = bean.getTypeData(); handler.sendEmptyMessage(HandlerOrder.UPDATE_LISTVIEW); } else { toastMsg = "��ȡά���������ʧ��"; handler.sendEmptyMessage(HandlerOrder.TOAST); } } }.start(); } // �ӱ��ػ�ȡͼƬ @SuppressLint("DefaultLocale") @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { 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.print("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; } } catch (IOException e) { e.printStackTrace(); } Log.d("dd", picturePath); } } private int picIndex = 1; private String img1Key = "img1"; private String img2Key = "img2"; 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; } /* * (non-Javadoc) * * @see android.app.Fragment#onDestroy() */ @Override public void onDestroy() { InputMethodManager imm = (InputMethodManager) ctx .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(null, 0); super.onDestroy(); } /** * ����δ���±����get����url * * @param title * @param type * @param tel * @param location * @param description * @param token * @param singnature * @param st * @return */ private String createHttpUrl(String title, String type, String tel, String location, String description, String token, String singnature, String st) { // ������ͬ��δ�����get��ʽ String http = "/admin/maintenance/addSave.do?title=" + title + "&type=" + type + "&tel=" + tel + "&location=" + location + "&description=" + description + "&token=" + token + "&singnature=" + singnature + "&st=" + st; for (int i = 0; i < uploadResultList.size(); i++) { http += "&attArry[" + i + "].filePath=" + uploadResultList.get(i).getAttachmentUrl(); http += "&attArry[" + i + "].fileName=" + uploadResultList.get(i).getAttachmentName(); http += "&attArry[" + i + "].fileSize=" + uploadResultList.get(i).getSize(); http += "&attArry[" + i + "].fileType=" + uploadResultList.get(i).getType(); } return http; } /** * ���Դ����±����getͬ������ * * @param title * @param type * @param tel * @param location * @param description * @param token * @param singnature * @param st * @return */ private String createHttpUrlWithEncoding(String title, String type, String tel, String location, String description, String token, String singnature, String st) { String http = "/admin/maintenance/addSave.do?token=" + token + "&singnature=" + singnature + "&st=" + st; List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); BasicNameValuePair b1 = new BasicNameValuePair("title", title); BasicNameValuePair b2 = new BasicNameValuePair("tel", tel); BasicNameValuePair b3 = new BasicNameValuePair("location", location); BasicNameValuePair b4 = new BasicNameValuePair("description", description); BasicNameValuePair bx = new BasicNameValuePair("type", type); params.add(b3); params.add(b2); params.add(b1); params.add(b4); params.add(bx); for (int i = 0; i < uploadResultList.size(); i++) { BasicNameValuePair b5 = new BasicNameValuePair("attArry[" + i + "].filePath", uploadResultList.get(i).getAttachmentUrl()); BasicNameValuePair b6 = new BasicNameValuePair("attArry[" + i + "].fileName", uploadResultList.get(i).getAttachmentName()); BasicNameValuePair b7 = new BasicNameValuePair("attArry[" + i + "].fileSize", uploadResultList.get(i).getSize()); BasicNameValuePair b8 = new BasicNameValuePair("attArry[" + i + "].fileType", uploadResultList.get(i).getType()); params.add(b5); params.add(b6); params.add(b7); params.add(b8); } http += "&" + OkHttpUtils.formatParams(params); System.out.println("format֮��IJ���" + OkHttpUtils.formatParams(params)); return http; } /** * �����첽post��ʽ�����ύ * * @param title * @param type * @param tel * @param location * @param description * @param token * @param singnature * @param st */ private void tryPost(String title, String type, String tel, String location, String description, String token, String singnature, String st) { // ����ʹ���첽post������� HashMap<String, String> map = new HashMap<String, String>(); // ���ڿ�ʼ������� map.put("title", title); map.put("type", type); map.put("tel", tel); map.put("location", location); map.put("description", description); 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/maintenance/addSave.do?token=" + token + "&singnature=" + singnature + "&st=" + st; handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW); OkHttpUtils.getInstance().doPostAsync(ctx, http, map, handler); } private void tryPost2(String title, String type, String tel, String location, String description, String token, String singnature, String st) { String json = "{\"title\":\"" + title + "\","; json += "\"type\":\"" + type + "\","; json += "\"tel\":\"" + tel + "\","; json += "\"location\":\"" + location + "\","; json += "\"description\":\"" + description + "\"}"; System.out.println(json); String url = "/admin/maintenance/addSave.do?token=" + token + "&singnature=" + singnature + "&st=" + st; String res = null; try { res = OkHttpUtils.getInstance().post(ctx, url, json); } catch (IOException e) { e.printStackTrace(); } // ����ֵ������JSON��ʽ�����ݣ���Ҫ��������� MyFixContentBean bean = JSON.parseObject(res, MyFixContentBean.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); } else { toastMsg = bean.getApp_result_message_key();// "��Ϣ�ύʧ�ܣ�������"; handler.sendEmptyMessage(HandlerOrder.TOAST); } } /** * ���һ�ε����Կ���HttpClient * * @return */ private void useHttpClient(String title, String type, String tel, String location, String description, String token, String singnature, String st) { String url = "/admin/maintenance/addSave.do?token=" + token + "&singnature=" + singnature + "&st=" + st; String ip = PropetiesFileReaderUtil.get(ctx, "ip"); String port = PropetiesFileReaderUtil.get(ctx, "port"); String server_project_name = PropetiesFileReaderUtil.get(ctx, "server_project_name"); if (!url.contains(server_project_name))// �����������̨��Ŀ�� { url = "/" + server_project_name + url; } String fullUrl = "http://" + ip + ":" + port + url; Map<String, String> params = new HashMap<String, String>(); params.put("title", title); params.put("type", type); params.put("tel", tel); params.put("location", location); params.put("description", description); String res = HttpXmlClient.post(fullUrl, params); // ����ֵ������JSON��ʽ�����ݣ���Ҫ��������� MyFixContentBean bean = JSON.parseObject(res, MyFixContentBean.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); } else { toastMsg = bean.getApp_result_message_key();// "��Ϣ�ύʧ�ܣ�������"; handler.sendEmptyMessage(HandlerOrder.TOAST); } } }