通过HTTP POST方法在android系统
问题描述:
它不是DUPLICATE.Link发送JSON对象,已提供的是一个古老的“HTTP客户端”已api23被删除通过HTTP POST方法在android系统
我想送JSON对象:
{"emailId":"[email protected]","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
强制性KEY:
{"emailId","address"}
REQUEST JSON:
{"emailId":"[email protected]","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"}
RESPONSE:
{"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;
}
}
+2
@gothdo你如何处理你从邮件中得到的回应? –
你所提到的API是一个GET方法。请向API开发人员提供有关访问API的正确方法的文档。我试着通过休息客户端向它发送一个POSt请求。它给出了一个错误。 – crashOveride
可能的重复[如何发送一个JSON对象通过Android的请求?](http://*.com/questions/3027066/how-to-send-a-json-object-over-request-with-android) –
我的不好!写错了网址! – user6092109