简体   繁体   中英

Invoking Rest Web services from Android?

I am very new Android rest web services.now i want connect my web-server using Restful Web-services for that i write the following code to open the wsdl file but i want connect to one single service from that server how can i do in 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);
        }
    }
}

By the Above code i am opening the wsdl file in the text area my requirement is connect to single services in the Wsdl please tel me how can i achieve it

Thanks in Advance......

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

At the end you will have response in String result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM