1

我已经使用 json 构建了一个 wcf 服务。我认为它可以正常工作,因为我一直在用 fiddler 调试它。虽然我不能从 android 正确调用它。我想我在尝试传递参数时一定做错了什么.这是提琴手请求:

请求标头:

用户代理:提琴手主机:androidwcf.schoolportal.gr 内容类型:application/json 内容长度:73

请求正文:

{ "ID":2147483647, "description":"字符串内容", "enable":true }

我如何将这个 json 对象作为参数传递给 android?感谢您的时间。

4

1 回答 1

1

尝试作为 ti 将当前 json obejct 发布到您的服务器:

    StringBuilder sb = new StringBuilder();  

    String http = "YOUR_WEB_URL";  
    //System.out.println("-----------------" + http+"?"+param);  
    HttpURLConnection urlConnection=null;  
    try {  
        URL url = new URL(http);  
         urlConnection = (HttpURLConnection) url  
                .openConnection();
        urlConnection.setDoOutput(true);   
        urlConnection.setRequestProperty("User-Agent", "Fiddler");  
        urlConnection.setRequestMethod("POST");  
        urlConnection.setUseCaches(false);  
        urlConnection.setConnectTimeout(10000);  
        urlConnection.setReadTimeout(10000);  
        urlConnection.setRequestProperty("Content-Type","application/json");   
        urlConnection.setRequestProperty("Content-Length","73");
        urlConnection.setRequestProperty("Host", "androidwcf.schoolportal.gr");
        urlConnection.connect();  

         //Create JSONObject here
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("ID", "25");
        jsonParam.put("description", "String content");
        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{  
            log.warn(urlConnection.getResponseMessage());  
        }  
    } catch (MalformedURLException e) {  

        e.printStackTrace();  
    }  
    catch (IOException e) {  

        e.printStackTrace();  
    }finally{  
        if(urlConnection!=null)  
           urlConnection.disconnect();  
    }  

}   
于 2012-12-05T16:19:52.143 回答