1

我正在尝试使用两种方法向 RESTful WCF Web 服务发出请求,但是在使用复杂类型参数访问该方法时遇到问题,它返回 HTML 格式的错误消息,而不是像没有参数的方法那样返回 XML。

这是我的代码:

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();
    }    

}

我做错了什么?

4

0 回答 0