从Android调用RESTWeb服务?

从Android调用RESTWeb服务?,android,web-services,rest,Android,Web Services,Rest,我是非常新的Android rest web services。现在我想使用Restful web services连接我的web服务器,为此我编写了以下代码以打开wsdl文件,但我想从该服务器连接到一个服务我在Android中该怎么做: package com.example.restservices; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; imp

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

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);
        }
    }
}
package com.example.restservices;
导入java.io.IOException;
导入java.io.InputStream;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.protocol.BasicHttpContext;
导入org.apache.http.protocol.HttpContext;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
公共类RestExampleActivity扩展了活动实现OnClickListener{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findviewbyd(R.id.my_按钮);
}
@凌驾
公共void onClick(视图arg0){
按钮b=(按钮)findViewById(R.id.my_按钮);
b、 可点击设置(假);
新建LongRunningGetIO().execute();
}
私有类LongRunningGetIO扩展异步任务{
受保护字符串GetAscioContentRomentity(HttpEntity实体)引发IllegalStateException、IOException{
InputStream in=entity.getContent();
StringBuffer out=新的StringBuffer();
int n=1;
而(n>0){
字节[]b=新字节[4096];
n=英寸读数(b);
如果(n>0)out.append(新字符串(b,0,n));
}
return out.toString();
}
@凌驾
受保护字符串doInBackground(无效…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpGet HttpGet=new HttpGet(“http://**************:8000/sap/bc/srt/wsdl/SRVC14dae9C8D79f1ee196f1fc6c6518a345/wsdl11/allinone/ws_策略/文档?sap客户端=800和sap用户=************和sap密码=********”);
字符串文本=空;
试一试{
HttpResponse response=httpClient.execute(httpGet,localContext);
HttpEntity=response.getEntity();
text=getAsciContentRomentity(实体);
}捕获(例外e){
返回e.getLocalizedMessage();
}
返回文本;
}   
受保护的void onPostExecute(字符串结果){
如果(结果!=null){
EditText et=(EditText)findViewById(R.id.my_edit);
et.setText(结果);
}
按钮b=(按钮)findViewById(R.id.my_按钮);
b、 可点击设置(真);
}
}
}
通过上面的代码,我正在文本区域中打开wsdl文件,我的需求是连接到wsdl中的单个服务。请告诉我如何实现它

提前谢谢

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();
}
最后,您将得到字符串结果中的响应