его НЕ ДУПЛИКАТ. Ссылка, которая была предоставлена, является СТАРОЙ. "http client" был удален в 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"}
На URL:
http://10digimr.mobimedia.in/api/mobile_retailer/update_profile Как это сделать? через почтовый метод?
МЕТОД:
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}
2 ответа
Определите класс 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;
}
}
@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;
}
}
Похожие вопросы
Связанные вопросы
Новые вопросы
android
Android — это мобильная операционная система Google, используемая для программирования или разработки цифровых устройств (смартфонов, планшетов, автомобилей, телевизоров, одежды, очков, IoT). Для тем, связанных с Android, используйте теги, специфичные для Android, такие как android-intent, android-activity, android-adapter и т. д. Для вопросов, отличных от разработки или программирования, но связанных с Android framework, используйте эту ссылку: https://android .stackexchange.com.