2 つのメソッドを使用して RESTful WCF Web サービスにリクエストを送信しようとしていますが、複雑な型パラメーターを使用してメソッドにアクセスするのに問題があり、パラメーターのないメソッドのような XML の代わりに HTML 形式のエラー メッセージが返されます。
これは私のコードです:
package com.example.testerestservice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONStringer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String SERVICE_URI = "http://10.0.2.2:9068/RestService.svc";
TextView txtResponse;
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public class ClientTask extends AsyncTask<Void, Void, String>
{
@Override
protected String doInBackground(Void... arg0) {
return addWithParameter();
}
@Override
protected void onPostExecute(String result) {
txtResposta.setText(result);
}
}
//this method NOT worksas I expect
public String addWithParameter() {
try
{
HttpPost request = new HttpPost(SERVICE_URI + "/AddParameter");
request.setHeader("Accept", "text/xml");
request.setHeader("Content-Type", "text/xml");
//This is as I try to represent the complex type to send
String number = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
number += "<NumberService>";
number += "<Number1>5</Number1>";
number += "<Number2>12</Number2>";
number += "</NumberService>";
StringEntity stringentity = new StringEntity(number.toString());
request.setEntity(stringentity);
//Envia requisição para o serviço WCF
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
return convertStreamToString(stream);
}
catch(Exception e)
{
return "Error: " + e.getMessage();
}
}
//this method works
public String addWithoutParameter() {
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(SERVICE_URI + "/Add");
request.setHeader("Accept", "application/xml");
request.setHeader("Content-type", "application/xml");
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if(entity != null) {
InputStream stream = entity.getContent();
return convertStreamToString(stream);
}
else
return null;
}
catch(Exception e)
{
return "Erro: " + e.getMessage();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtResponse = (TextView) findViewById(R.id.lblResposta);
new ClientTask().execute();
}
}
私が間違っていることは何ですか?