【问题标题】:Sending json object via http post method in android在android中通过http post方法发送json对象
【发布时间】:2016-04-25 06:54:21
【问题描述】:

它不重复。提供的链接是旧链接。“http 客户端”已在 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 我该怎么做? 通过post方式?

方法:

 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}

【问题讨论】:

  • 你提到的api是GET方法。向 API 开发人员索取有关访问 API 的正确方法的文档。我尝试通过休息客户端向它发送一个 POST 请求。它给出了一个错误。
  • 我的错!网址写错了!
  • 其不重复。提供的链接是旧链接。“http 客户端”已在 api23 中删除

标签: android json http-post httprequest httpurlconnection


【解决方案1】:

定义一个类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;
        }


    }

【讨论】:

  • @gothdo 你如何处理从帖子中得到的回复?
【解决方案2】:

@Sandip Subedi 这是您从 httpURLConnection 获得响应的方式

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();

            InputStream response = httpURLConnection.getInputStream();
            BufferedReader reader = new BufferedReader(newInputStreamReader(response);
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                   is.close();
                } catch (IOException e) {
                   e.printStackTrace();
                }
            }

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

        return null;
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多