I'm using the code below to send an http POST request which sends an object to a WCF service. This works ok, but what happens if my WCF service needs also other parameters? How can I send them from my Android client?

这是我到目前为止写的代码:

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();  
}  

推荐答案

使用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");

The part which you missed is in the the following... i.e., as follows..

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

The rest of the thing you can do it.

Json相关问答推荐

如何在PowerShell中扩展JSON中的嵌套数组

将JSON输入子数组转换为字符串顺序列表输出

当console.log返回TRUE值时,解析的JSON中的字段未定义

在MongoDB中检索某个月份和年份的JSON文档

bash用jq获取第二条JSON记录

NoneType 对象的 Python 类型错误

jq 对特定键进行过滤并将值整理到单个 csv 单元格中

使用 Power BI 中的 Deneb 视觉效果绘制面积图中的 X 轴日期

Python 将 struct 化文本转换和筛选为对象

修改 boost::json::object 中的值

如何将该 JSON 解析为 Kotlin 类?

如果 jq 数组中的字符串是对象或字符串,则获取值

在循环中将变量添加到 bash 数组

PowerShell - 如何迭代 PSCustomObject 嵌套对象?

使用 GSON 解析嵌套的 JSON 数据

如何在 MVC4 中将 Json 字符串输出为 JsonResult?

有什么方法可以判断您安装的 gulp 版本吗?

有 Json 标签但未导出

强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

将 JSON 导出到环境变量