/** * 启动线程向服务器post提交数据 */ public void upLoadPic(final Intent data) { ToastUtils.toastWithMessage(getActivity(), "正在上传..."); new Thread(new Runnable() { @Override public void run() { httpClient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(PATH_STRING); // 添加内容体,图片,以及文本两部分 MultipartEntity mpEntity = new MultipartEntity(); ContentBody imgFile = new FileBody(tempFile, "image/*"); // 内容体中的id if (map != null && !map.isEmpty()) { for (Map.Entry<String, String> entry : map.entrySet()) { try { mpEntity.addPart(entry.getKey(), new StringBody( entry.getValue())); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } // 图片部分 mpEntity.addPart("img", imgFile); httppost.setEntity(mpEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response; try { response = httpClient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity resEntity = response.getEntity(); // 得到json结果,解析 String result = EntityUtils.toString(resEntity, HTTP.UTF_8); JSONObject jsonObject = new JSONObject(result); JSONObject dataObject = jsonObject .getJSONObject("data"); int result_code = dataObject.getInt("result"); if (result_code == 0) { storePicExternal(data, AVATAR_IMAGE_FILE_NAME); tempFile.delete(); } Message message = Message.obtain(); message.what = MSG_PIC; message.obj = result_code; handler.sendMessage(message); System.out.println(response.getStatusLine()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }).start(); }
-------------
提交文本信息:
/** * 启动线程向服务器post提交数据 */ public void updateInfo() { ToastUtils.toastWithMessage(getActivity(), "正在提交.."); list.add(new BasicNameValuePair("id", userId)); String location = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE).getString("location", null); if (location != null) { list.add(new BasicNameValuePair("location", location)); } String name = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE).getString("userName", null); if (name != null) { list.add(new BasicNameValuePair("name", name)); } list.add(new BasicNameValuePair("sign", sign.getText().toString() .trim())); new Thread(new Runnable() { @Override public void run() { HttpClient httpClient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(UPDATE_PATH); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response; try { httppost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); response = httpClient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity resEntity = response.getEntity(); // 得到json结果,解析 String result = EntityUtils.toString(resEntity, HTTP.UTF_8); JSONObject jsonObject = new JSONObject(result); JSONObject dataObject = jsonObject .getJSONObject("data"); int result_code = dataObject.getInt("result"); Message message = Message.obtain(); message.what = MSG_INFO; message.obj = result_code; handler.sendMessage(message); System.out.println(response.getStatusLine()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } } }).start(); }
---------------------
提交信息的结果需要由服务器端返回一些信息,此次返回的信息为:1,提交失败;0,提交成功。
json数据格式