从Android发送JSON HTTP POST请求

我正在使用下面的代码发送一个HTTP POST请求发送一个对象到WCF服务。 这工作正常,但如果我的WCF服务还需要其他参数会发生什么? 我怎样才能从我的Android客户端发送它们?

这是我迄今写的代码:

StringBuilder sb = new StringBuilder(); String 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.toString()); out.close(); int HttpResult =urlConnection.getResponseCode(); if(HttpResult ==HttpURLConnection.HTTP_OK){ BufferedReader br = new BufferedReader(new InputStreamReader( urlConnection.getInputStream(),"utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); System.out.println(""+sb.toString()); }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(); } 

2 Solutions collect form web for “从Android发送JSON HTTP POST请求”

使用POST发布参数: –

 URL url; URLConnection urlConn; DataOutputStream printout; DataInputStream input; url = new URL (getCodeBase().toString() + "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"); 

您错过的部分在以下…即,如下所示。

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

剩下的事情你可以做到。

尝试一些像打击的事情:

 SString otherParametersUrServiceNeed = "Company=acompany&Lng=test&MainPeriod=test&UserID=123&CourseDate=8:10:10"; String 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.toString(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.toString()); wr.flush(); wr.close(); 

参考文献

  1. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
  2. Java – 通过POST方法轻松发送HTTP参数
  • 我应该使用netsh.exe什么appid?
  • 何时使用DataContract和DataMember属性?
  • 一个asp.net web方法和一个wcf服务有什么区别?
  • 通过VBScript调用WCF服务
  • 无法添加对dll的引用
  • 如何以编程方式configurationWCF已知types?
  • 如何处理WCFexception(带有代码的统一列表)
  • “这个错误的创造者没有指定一个理由”exception
  • DataContract XML序列化和XML属性
  • 在使用Windows服务和SQL Server的OneWay WCF消息中排队
  • 什么是WCF客户端`使用`块问题的最佳解决方法?