Android 資料上傳(上傳圖片的時候同時上傳圖片名,拍照時間之類的)
阿新 • • 發佈:2018-11-10
// 上傳檔案public void upload(File file) {try {// post請求需要攜帶的引數AjaxParams params = new AjaxParams();// 將檔案放入引數中LoginInfo loginInfo = new LoginInfo();loginInfo.userToken = mSharedPreferences.getString("userToken", "");loginInfo.deviceId = Utils.getDeviceId(mContext);params.put("userToken", loginInfo.userToken);params.put("deviceId", loginInfo.deviceId);params.put("pictureType", "001");params.put("file", file);// 請求的URLString url = HttpMethodUtils.UPLOADPICTUREURLSERVICE;// post請求,三個引數分別是請求地址、請求引數、請求的回撥介面fh = new FinalHttp();fh.post(url, params, new AjaxCallBack() {@Overridepublic void onFailure(Throwable t, int errorNo, String strMsg) {super.onFailure(t, errorNo, strMsg);System.out.println();Toast.makeText(mContext, "上傳失敗", 0).show();}@Overridepublic void onStart() {super.onStart();System.out.println();// Toast.makeText(mContext, "開始上傳", 0).show();}@Overridepublic void onSuccess(String t) {super.onSuccess(t);// 根據伺服器返回的json資料,判斷上傳是否成功Toast.makeText(mContext, "上傳成功", 0).show();getPersonInfoTask();if (!TextUtils.isEmpty(t)) {try {JSONObject obj = new JSONObject(t);if (obj.optInt("result") == 1) {Toast.makeText(mContext, "成功", 0).show();}} catch (Exception e) {e.printStackTrace();}}}});} catch (Exception e) {e.printStackTrace();}}