我有一个'wcf'休息服务,它接收一个自定义类作为输入参数。我想通过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());
答案 0 :(得分: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
答案 1 :(得分:0)
我终于找到了答案。 在HttpUrlConnection中,您不应该关闭输出流beform读取输入流。所以我删除了所有输出流关闭。