Я хочу опубликовать данные ниже json, используя AsyncTask
... Моя проблема в том, что у меня есть ключ, называемый dynamic_fields
значения JSON Format
Но он хранится в формате String
ПримерДанные JSON
{
"createdDate": Fri Mar 15 12:00:59 GMT+05:30 2019,
"UID": "",
"role": "manager",
"webMenu": [],
"mobileMenu": {},
"isAdmin": false,
"isDeleted": false,
"tasks": {
"tasks": 0,
"inProgress": 0,
"total": 0
},
"dynamic_fields": {
"location": "Hyderabad",
"area": "KPHB"
}
}
Ниже снимок экрана изменения формата JSON на строку в базе данных
MyAsyncTask
private void createEmployee() {
employeeDetails.put("email", email);
employeeDetails.put("phoneNumber", mobile);
employeeDetails.put("firstName", firstName);
employeeDetails.put("lastName", "-");
employeeDetails.put("photoUrl", photoUrl);
employeeDetails.put("isManager", isManager);
employeeDetails.put("OID", OID);
employeeDetails.put("MID", userId);
employeeDetails.put("address", "-");
if (hashMap_dynamicForms != null && hashMap_dynamicForms.size() >0) {
JSONObject jsonObject = new JSONObject(hashMap_dynamicForms);
employeeDetails.put("dynamic_fields", jsonObject);
}
Log.e("data", employeeDetails+"");
new SignupAsynTask().execute();
}
class SignupAsynTask extends AsyncTask<String,String,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
RegisterUser_Signup objreg = new RegisterUser_Signup();
HashMap<String, String> data = new HashMap<String, String>();
// ArrayList<Object> arrays = new ArrayList<>();
// arrays.add(employeeDetails);
String result = null;
try {
result = objreg.sendPostRequest(RequestUrl, employeeDetails);
}catch (Exception e) {
Log.d("Exception", "errortaskcare" + e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
hideProgressDialg(Create_NewEmployee.this);
if (s.contains("Error Registering")) {
Toast.makeText(Create_NewEmployee.this, "Error, please try again", Toast.LENGTH_SHORT).show();
return;
} else {
}
}
}
Метод sendPostRequest
public String sendPostRequest(String requestURL,
HashMap<String, Object> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, Object> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, Object> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"));
}
return result.toString();
}