将json作为对象从android发送到wcf

将json作为对象从android发送到wcf,android,wcf,httpurlconnection,postdata,Android,Wcf,Httpurlconnection,Postdata,我有一个“wcf”rest服务,它接收一个自定义类作为输入参数。我想通过android平台发送这个类对象数据。我使用了httpUrlConnection。我总是收到回复代码500。但该服务在windows窗体应用程序中正常工作。我的android代码如下: HttpURLConnection urlConnection = null; String jsonString = ""; JSONObject jsonObject = new JSONO

我有一个“wcf”rest服务,它接收一个自定义类作为输入参数。我想通过android平台发送这个类对象数据。我使用了
httpUrlConnection
。我总是收到回复代码500。但该服务在windows窗体应用程序中正常工作。我的android代码如下:

        HttpURLConnection urlConnection = null;
        String jsonString = "";
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        try {

            jsonObject.put("A","rtrt");
            jsonObject2.put("c",jsonObject);
               } catch (JSONException e) {
                 e.printStackTrace();
               }


        try{
            URL url = new URL("http://.../Service1.svc/GetTestData");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST");
              urlConnection.setRequestProperty("ContentType","applicaiton/json");


          urlConnection.setUseCaches(false);

      urlConnection.connect();

            OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            writer.write(jsonObject2.toString());
            writer.flush();
            writer.close();
            outputStream.close();

       int statusCode = urlConnection.getResponseCode();

           // Log.d("TRAFFIC", "err: " + urlConnection.getErrorStream().toString());

Wcf接受SOAP类型数据

使用图书馆


完整教程可用

Wcf接受SOAP类型数据

使用图书馆


我终于找到了答案。
在HttpUrlConnection中,不应在读取inputstream之前关闭outputstream。所以我删除了所有outputstream关闭

我终于找到了答案。
在HttpUrlConnection中,不应在读取inputstream之前关闭outputstream。所以我删除了所有outputstream关闭

这是不正确的。如果您更改绑定并将其设置为webhttpbinding,Wcf支持rest。我是.Net和Android开发者,很久以前就遇到过这个问题,Ksoap2是解决方案。这是不正确的。如果您更改绑定并将其设置为webhttpbinding,Wcf支持rest。我是.Net和Android开发者,早在几天前就面临这个问题,Ksoap2就是解决方案。
public class ksop2test extends Activity {
 /** Called when the activity is first created. */


 private static final String METHOD_NAME = "HelloWorldRequest";
//  private static final String METHOD_NAME = "HelloWorld";

private static final String NAMESPACE = "http://tempuri.org/";
//  private static final String NAMESPACE = "http://tempuri.org";

private static final String URL ="http://192.168.0.2:8080/HelloWCF/Service1.svc";
//  private static final String URL = "http://192.168.0.2:8080/webservice1  /Service1.asmx";

final String SOAP_ACTION = "http://tempuri.org/IService1/HelloWorld";
//  final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
TextView tv;
StringBuilder sb;
private XmlSerializer writer;



@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  tv = new TextView(this);
  sb = new StringBuilder();
  call();
  tv.setText(sb.toString());
  setContentView(tv);
}

public void call() {
 try {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("Name", "Qing");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);


    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

    //to get the data
    String resultData = result.toString();
    // 0 is the first object of data 


    sb.append(resultData + "\n");
    } catch (Exception e) {
    sb.append("Error:\n" + e.getMessage() + "\n");
    }

}

}