Android使用Apache的httpmime包post提交数据

    /**
     * 启动线程向服务器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();

    }
View Code

-------------

提交文本信息:

/**
     * 启动线程向服务器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();
    }
View Code

---------------------

提交信息的结果需要由服务器端返回一些信息,此次返回的信息为:1,提交失败;0,提交成功。

json数据格式

转载于:https://www.cnblogs.com/paulFrank/p/3204478.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值