android - 在android中通过http post方法发送json对象

标签 android json http-post httprequest httpurlconnection

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

我要发送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"}

到网址:

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 方法中调用它:

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 - 在android中通过http post方法发送json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36833798/

相关文章:

java - 如何从Web读取JSON数据并将插入到数据库中

java - 滑动时的Android Scrollable Tabs + Swipe状态

java - 解析 JSON 并从中删除对象会出错

javascript - 使用 $.each() 解析复杂的 JSON

json - json格式无效,请检查。原因 : invalid character 'a' looking for beginning of object key string

php - 使用 AFHTTPRequestOperationManager 的 HTTP 请求

iphone - 带参数的 HTTP POST 请求

android - 如何使用 mylocationoverlay 将我的位置居中?

android - Android 中的 XPATH

java - Android 帧动画被强制停止。需要理由