2016-10-19 50 views
1

我有一個接收自定義類作爲輸入參數的'wcf'rest服務。我想通過android平臺發送這個類的對象數據。我用httpUrlConnection。我總是收到響應代碼500.但服務在Windows窗體應用程序中正常工作。我在Android的代碼是在這裏:將json作爲對象發送給wcf from 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()); 

回答

0

WCF接受SOAP數據類型

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

} 

} 

完全教程可用here

+0

這是不正確的。如果更改綁定並將其設置爲webhttpbinding,則Wcf支持休息。 –

+0

我是.Net以及Android開發人員,很久以前就面臨這個問題,Ksoap2是解決方案。 –

0

我終於找到了答案。 在HttpUrlConnection中,您不應關閉outputstream beform讀取輸入流。所以我刪除了所有輸出流關閉。