0

我是非常新的 Android REST Web 服务。现在我想使用 Restful Web 服务连接我的 Web 服务器,为此我编写以下代码来打开 wsdl 文件,但我想从该服务器连接到一个服务我该怎么做安卓 :

package com.example.restservices;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RestExampleActivity extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Button b = (Button)findViewById(R.id.my_button);
        b.setClickable(false);
        new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask <Void, Void, String> {

        protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
           InputStream in = entity.getContent();
             StringBuffer out = new StringBuffer();
             int n = 1;
             while (n>0) {
                 byte[] b = new byte[4096];
                 n =  in.read(b);
                 if (n>0) out.append(new String(b, 0, n));
             }
             return out.toString();
        }

        @Override
        protected String doInBackground(Void... params) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();
             HttpGet httpGet = new HttpGet("http://*************:8000/sap/bc/srt/wsdl/srvc_14DAE9C8D79F1EE196F1FC6C6518A345/wsdl11/allinone/ws_policy/document?sap-client=800&sap-user=***********&sap-password=************");
             String text = null;
             try {
                   HttpResponse response = httpClient.execute(httpGet, localContext);
                   HttpEntity entity = response.getEntity();
                   text = getASCIIContentFromEntity(entity);
             } catch (Exception e) {
                 return e.getLocalizedMessage();
             }
             return text;
        }   

        protected void onPostExecute(String results) {
            if (results!=null) {
                EditText et = (EditText)findViewById(R.id.my_edit);
                et.setText(results);
            }
            Button b = (Button)findViewById(R.id.my_button);
            b.setClickable(true);
        }
    }
}

通过上面的代码,我在文本区域中打开 wsdl 文件,我的要求是连接到 Wsdl 中的单个服务,请告诉我如何实现它

提前致谢......

4

1 回答 1

1
try {
    URL sourceUrl = new URL(url);
    is = sourceUrl.openStream();
} catch (Exception e) {
    e.printStackTrace();
}

try {
    StringBuilder out = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String tempStr = "";

    while ((tempStr = br.readLine()) != null) {
        out.append(tempStr);
    }
    result = out.toString();
} catch (Exception e) {
    e.printStackTrace();
}

最后,您将在字符串结果中得到响应。

于 2012-08-21T06:28:47.457 回答