I am trying to send json encoded string(params[0]) to the webserver with some header content using asyncTask. In the web server i am receiving headers but not receiving json encoded string and getting request code 200. Here is my BackgroundTask class:
@Override
protected String doInBackground(String... params) {
try {
URL url=new URL(jsonurl);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Length",
Integer.toString(params[0].getBytes().length));
httpURLConnection.setRequestProperty("Accept", "application/json");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
ResponseCode=httpURLConnection.getResponseCode();
OutputStream OS=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data= URLEncoder.encode(params[0], "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS=httpURLConnection.getInputStream();
return "data saved";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "not saved into server";
}
and here is call to the BackgroundTask:
BackgroundTask backgroundTask= new BackgroundTask(this);
JSONObject jsonObject=new JSONObject();
JSONArray jsonArray=new JSONArray();
try {
jsonObject.put("mail",Email );
jsonObject.put("name", Name);
jsonObject.put("surname", Surname);
jsonObject.put("password",Password);
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(jsonObject.toString());
backgroundTask.execute(jsonObject.toString());
Aucun commentaire:
Enregistrer un commentaire