Envoi d’une requête JSON HTTP POST à ​​partir d’Android

J’utilise le code ci-dessous pour envoyer une requête HTTP POST qui envoie un object à un service WCF. Cela fonctionne bien, mais que se passe-t-il si mon service WCF nécessite également d’autres parameters? Comment puis-je les envoyer depuis mon client Android?

C’est le code que j’ai écrit jusqu’à présent:

SsortingngBuilder sb = new SsortingngBuilder(); Ssortingng http = "http://android.schoolportal.gr/Service.svc/SaveValues"; HttpURLConnection urlConnection=null; try { URL url = new URL(http); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); urlConnection.setUseCaches(false); urlConnection.setConnectTimeout(10000); urlConnection.setReadTimeout(10000); urlConnection.setRequestProperty("Content-Type","application/json"); urlConnection.setRequestProperty("Host", "android.schoolportal.gr"); urlConnection.connect(); //Create JSONObject here JSONObject jsonParam = new JSONObject(); jsonParam.put("ID", "25"); jsonParam.put("description", "Real"); jsonParam.put("enable", "true"); OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream()); out.write(jsonParam.toSsortingng()); out.close(); int HttpResult =urlConnection.getResponseCode(); if(HttpResult ==HttpURLConnection.HTTP_OK){ BufferedReader br = new BufferedReader(new InputStreamReader( urlConnection.getInputStream(),"utf-8")); Ssortingng line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); System.out.println(""+sb.toSsortingng()); }else{ System.out.println(urlConnection.getResponseMessage()); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(urlConnection!=null) urlConnection.disconnect(); } 

Paramètres de publication à l’aide de POST: –

 URL url; URLConnection urlConn; DataOutputStream printout; DataInputStream input; url = new URL (getCodeBase().toSsortingng() + "env.tcgi"); urlConn = url.openConnection(); urlConn.setDoInput (true); urlConn.setDoOutput (true); urlConn.setUseCaches (false); urlConn.setRequestProperty("Content-Type","application/json"); urlConn.setRequestProperty("Host", "android.schoolportal.gr"); urlConn.connect(); //Create JSONObject here JSONObject jsonParam = new JSONObject(); jsonParam.put("ID", "25"); jsonParam.put("description", "Real"); jsonParam.put("enable", "true"); 

La partie que vous avez manquée est la suivante … c’est-à-dire comme suit ..

 // Send POST output. printout = new DataOutputStream(urlConn.getOutputStream ()); printout.writeBytes(URLEncoder.encode(jsonParam.toSsortingng(),"UTF-8")); printout.flush (); printout.close (); 

Le rest de la chose, vous pouvez le faire.

essayer quelque chose comme coup:

 SSsortingng otherParametersUrServiceNeed = "Company=acompany&Lng=test&MainPeriod=test&UserID=123&CourseDate=8:10:10"; Ssortingng request = "http://android.schoolportal.gr/Service.svc/SaveValues"; URL url = new URL(request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Content-Length", "" + Integer.toSsortingng(otherParametersUrServiceNeed.getBytes().length)); connection.setUseCaches (false); DataOutputStream wr = new DataOutputStream(connection.getOutputStream ()); wr.writeBytes(otherParametersUrServiceNeed); JSONObject jsonParam = new JSONObject(); jsonParam.put("ID", "25"); jsonParam.put("description", "Real"); jsonParam.put("enable", "true"); wr.writeBytes(jsonParam.toSsortingng()); wr.flush(); wr.close(); 

Les références :

  1. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
  2. Java – envoyer facilement des parameters HTTP via la méthode POST