在android中通过http post方法发送json对象 [英] Sending json object via http post method in android

查看:1446
本文介绍了在android中通过http post方法发送json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


已提供的NOT DUPLICATE.Link是旧版本。http客户端已在api23中删除

its NOT DUPLICATE.Link that has been provided is an OLD one."http client" has been removed in api23

我想发送json对象:

I want to send json object:

{"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"}

到网址:


http://10digimr.mobimedia.in / api / mobile_retailer / update_profile
我该怎么做?
通过邮寄方式?

http://10digimr.mobimedia.in/api/mobile_retailer/update_profile How do i do it? via post method?

方法:

 POST /api/mobile_retailer/update_profile

强制性密钥:

{"emailId","address"}

请求JSON:

{"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"}

回复:

{"message":"Mail Send","data":true,"status":200}


推荐答案

定义一个类 AsyncT 并在 onCreate 方法使用:

Define a class AsyncT and call it in onCreate method using:

AsyncT asyncT = new AsyncT();
asyncT.execute();

班级定义:

class AsyncT extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params) {

            try {
                URL url = new URL(""); //Enter URL here
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setRequestMethod("POST"); // here you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
                httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
                httpURLConnection.connect();

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("para_1", "arg_1");

                DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
                wr.writeBytes(jsonObject.toString());
                wr.flush();
                wr.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


    }

这篇关于在android中通过http post方法发送json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆