當前位置:首頁 » 網頁前端 » android調用web
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

android調用web

發布時間: 2022-10-16 22:59:35

㈠ android怎麼調用webservice

publicclass WebService extends Activity {
privatestaticfinal String NAMESPACE ="http://WebXml.com.cn/";
// WebService地址
privatestatic String URL ="http://www.webxml.com.cn/
webservices/weatherwebservice.asmx";
privatestaticfinal String METHOD_NAME ="getWeatherbyCityName";
privatestatic String SOAP_ACTION ="http://WebXml.com.cn/
getWeatherbyCityName";

private String weatherToday;

private Button okButton;
private SoapObject detail;

@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.ok);

okButton.setOnClickListener(new Button.OnClickListener() {
publicvoid onClick(View v) {
showWeather();
}
});
}

privatevoid showWeather() {
String city ="武漢";
getWeather(city);
}

@SuppressWarnings("deprecation")
publicvoid getWeather(String cityName) {
try {
System.out.println("rpc------");
SoapObject rpc =new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("rpc"+ rpc);
System.out.println("cityName is "+ cityName);
rpc.addProperty("theCityName", cityName);

AndroidHttpTransport ht =new AndroidHttpTransport(URL);
ht.debug =true;

SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.bodyOut = rpc;
envelope.dotNet =true;
envelope.setOutputSoapObject(rpc);

ht.call(SOAP_ACTION, envelope);

SoapObject result = (SoapObject) envelope.bodyIn;
detail = (SoapObject) result
.getProperty("getWeatherbyCityNameResult");

System.out.println("result"+ result);
System.out.println("detail"+ detail);
Toast.makeText(WebService.this, detail.toString(),
Toast.LENGTH_LONG).show();
parseWeather(detail);

return;
} catch (Exception e) {
e.printStackTrace();
}
}

privatevoid parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday ="今天:"+ date.split("")[0];
weatherToday = weatherToday +"\n天氣:"+ date.split("")[1];
weatherToday = weatherToday +"\n氣溫:"
+ detail.getProperty(5).toString();
weatherToday = weatherToday +"\n風力:"
+ detail.getProperty(7).toString() +"\n";
System.out.println("weatherToday is "+ weatherToday);
Toast.makeText(WebService.this, weatherToday,
Toast.LENGTH_LONG).show();

}
}

㈡ android怎樣調用webService

//WebService的命名空間
static final String namespace = "http://impl.service.suncreate.com";
//伺服器發布的url
static final String url = http://10.100.3.41/axis2/services/UploadService;
final String methodName = "upload"; // 函數名
final int sessionID = "111111"; //sessionID
//創建HttpTransportSE對象,通過HttpTransportSE類的構造方法可以指定WebService的url
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
//指定WebService的命名空間和函數名
SoapObject soapObject = new SoapObject(namespace, methodName);
//設置調用方法參數的值
soapObject.addProperty("sessionID", sessionID); //sessionID
soapObject.addProperty("data", cds); //cds是需要傳遞的對象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);
//使用call方法調用WebService方法
transport.call(null, envelope);
SoapObject sb = (SoapObject) envelope.bodyIn;
String xmlMessage = sb.toString(); // 獲取從伺服器端返回的XML字元串

㈢ android平台調用webservice介面

在Android平台調用Web Service需要依賴於第三方類庫ksoap2,它是一個SOAP Web service客戶端開發包,主要用於資源受限制的Java環境如Applets或J2ME應用程序(CLDC/ CDC/MIDP)。認真讀完對ksoap2的介紹你會發現並沒有提及它應用於Android平台開發,沒錯,在Android平台中我們並不會直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平台上一個高效、輕量級的SOAP開發包,等同於Android平台上的KSoap2的移植版本。

㈣ android 調用webservice方式有哪些

使用Ksoup.jar包可以實現webservice的調用
參考代碼:
String result = null;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.bodyOut = soapObject;
String endPoint = 地址後綴//如WebService/AppService.asmx
HttpTransportSE transportSE = new HttpTransportSE(endPoint);
SoapObject object = null;
transportSE.call(地址 + soapObject.getName(),
envelope);
object = (SoapObject) envelope.bodyIn;
result = object.getProperty(0).toString();

附上ksoup包

㈤ android下打開Web瀏覽器的幾種常見的方法

android下打開Web瀏覽器的幾種常見的方法如下:

一。通過意圖實現瀏覽

//通過下述方法打開瀏覽器

privatevoidopenBrowser(){
//urlText是一個文本輸入框,輸入網站地址
//Uri是統一資源標識符
Uriuri=Uri.parse(urlText.getText().toString());
Intentintent=newIntent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}

注意:輸入URL時,不要忘記「http://」部分。

二。利用視圖打開網頁,是通過調用WebKit瀏覽器引擎提供的WebView實現的。

具體源代碼如下:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayoutandroid:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:layout_width="240dp"
android:layout_height="wrap_content"
android:id="@+id/etWebSite"
android:hint="輸入網址"
android:singleLine="true"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/searchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
android:layout_marginRight="5dp"
/>
</LinearLayout>
<LinearLayoutandroid:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一頁"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一頁"
android:layout_marginRight="5dp"
/>
</LinearLayout>
<WebViewandroid:id="@+id/webView1"android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
/res/src/com.myandroid


packagecom.myandroid;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.webkit.URLUtil;
importandroid.webkit.WebView;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
{

privateButtonschBtn,backBtn,nextBtn;
privateWebViewwebView;
privateEditTextmText;

@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

schBtn=(Button)findViewById(R.id.searchBtn);
mText=(EditText)findViewById(R.id.etWebSite);
webView=(WebView)findViewById(R.id.webView1);
backBtn=(Button)findViewById(R.id.backBtn);
nextBtn=(Button)findViewById(R.id.nextBtn);
schBtn.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
//設置可以使用Javascript
webView.getSettings().setJavaScriptEnabled(true);StringstrURI=mText.getText().toString();
//檢測網站的合法性
if(URLUtil.isNetworkUrl(strURI)){
webView.loadUrl(strURI);
Toast.makeText(WebViewActivity.this,strURI,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(WebViewActivity.this,"輸入非法網站 "+strURI,Toast.LENGTH_SHORT).show();
}
}
});

backBtn.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
if(webView.canGoBack()){
webView.goBack();
}
}
});

nextBtn.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
if(webView.canGoForward()){
webView.goForward();
}
}
});
}
}


同時還要在AndroidManifest.xml中添加訪問網際網路的許可權:

<uses-permission android:name="android.permission.INTERNET"/>

㈥ android端怎麼調用webservice介面

在Android平台調用Web Service需要依賴於第三方類庫ksoap2,它是一個SOAP Web service客戶端開發包,主要用於資源受限制的Java環境如Applets或J2ME應用程序(CLDC/ CDC/MIDP)。認真讀完對ksoap2的介紹你會發現並沒有提及它應用於Android平台開發,沒錯,在Android平台中我們並不會直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平台上一個高效、輕量級的SOAP開發包,等同於Android平台上的KSoap2的移植版本。
Ksoap2-android jar包下載

㈦ 安卓怎麼調用webservice

具體調用調用webservice的方法為:
(1) 指定webservice的命名空間和調用的方法名,如:
SoapObject request =new SoapObject(http://service,」getName」);
SoapObject類的第一個參數表示WebService的命名空間,可以從WSDL文檔中找到WebService的命名空間。第二個參數表示要調用的WebService方法名。
(2) 設置調用方法的參數值,如果沒有參數,可以省略,設置方法的參數值的代碼如下:
Request.addProperty(「param1」,」value」);
Request.addProperty(「param2」,」value」);
要注意的是,addProperty方法的第1個參數雖然表示調用方法的參數名,但該參數值並不一定與服務端的WebService類中的方法參數名一致,只要設置參數的順序一致即可。
(3) 生成調用Webservice方法的SOAP請求信息。該信息由SoapSerializationEnvelope對象描述,代碼為:
SoapSerializationEnvelope envelope=new
SoapSerializationEnvelope(SoapEnvelope.VER11);
Envelope.bodyOut = request;
創建SoapSerializationEnvelope對象時需要通過SoapSerializationEnvelope類的構造方法設置SOAP協議的版本號。該版本號需要根據服務端WebService的版本號設置。在創建SoapSerializationEnvelope對象後,不要忘了設置SOAPSoapSerializationEnvelope類的bodyOut屬性,該屬性的值就是在第一步創建的SoapObject對象。
(4) 創建HttpTransportsSE對象。通過HttpTransportsSE類的構造方法可以指定WebService的WSDL文檔的URL:
HttpTransportSE ht=new HttpTransportSE(「SearchNewsService?wsdl」);
(5)使用call方法調用WebService方法,代碼:
ht.call(null,envelope);
Call方法的第一個參數一般為null,第2個參數就是在第3步創建的SoapSerializationEnvelope對象。
(6)使用getResponse方法獲得WebService方法的返回結果,代碼:
SoapObject soapObject =( SoapObject) envelope.getResponse();
以下為簡單的實現一個天氣查看功能的例子:
復制代碼
publicclass WebService extends Activity {
privatestaticfinal String NAMESPACE ="";
// WebService地址
privatestatic String URL ="weatherwebservice.asmx";
privatestaticfinal String METHOD_NAME ="getWeatherbyCityName";
privatestatic String SOAP_ACTION ="getWeatherbyCityName";
private String weatherToday;
private Button okButton;
private SoapObject detail;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(new Button.OnClickListener() {
publicvoid onClick(View v) {
showWeather();
}
});
}
privatevoid showWeather() {
String city ="武漢";
getWeather(city);
}
@SuppressWarnings("deprecation")
publicvoid getWeather(String cityName) {
try {
System.out.println("rpc------");
SoapObject rpc =new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("rpc"+ rpc);
System.out.println("cityName is "+ cityName);
rpc.addProperty("theCityName", cityName);
AndroidHttpTransport ht =new AndroidHttpTransport(URL);
ht.debug =true;
SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet =true;
envelope.setOutputSoapObject(rpc);
ht.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
detail = (SoapObject) result
.getProperty("getWeatherbyCityNameResult");
System.out.println("result"+ result);
System.out.println("detail"+ detail);
Toast.makeText(WebService.this, detail.toString(),
Toast.LENGTH_LONG).show();
parseWeather(detail);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
privatevoid parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday ="今天:"+ date.split("")[0];
weatherToday = weatherToday +"\n天氣:"+ date.split("")[1];
weatherToday = weatherToday +"\n氣溫:"
+ detail.getProperty(5).toString();
weatherToday = weatherToday +"\n風力:"
+ detail.getProperty(7).toString() +"\n";
System.out.println("weatherToday is "+ weatherToday);
Toast.makeText(WebService.this, weatherToday,
Toast.LENGTH_LONG).show();
}
}

㈧ android怎麼調用webservice

使用Ksoup.jar包可以實現webservice的調用
參考代碼:
String result = null;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.bodyOut = soapObject;
String endPoint = 地址後綴//如WebService/AppService.asmx
HttpTransportSE transportSE = new HttpTransportSE(endPoint);
SoapObject object = null;
transportSE.call(地址 + soapObject.getName(),
envelope);
object = (SoapObject) envelope.bodyIn;
result = object.getProperty(0).toString();

㈨ android怎麼調用webservice

WebService是一種基於SOAP協議的遠程調用標准,通過webservice可以將不同操作系統平台、不同語言、不同技術整合到一塊。在Android SDK中並沒有提供調用WebService的庫,因此,需要使用第三方的SDK來調用WebService。PC版本的WEbservice客戶端庫非常豐富,例如Axis2,CXF等,但這些開發包對於Android系統過於龐大,也未必很容易移植到Android系統中。因此,這些開發包並不是在我們的考慮范圍內。適合手機的WebService客戶端的SDK有一些,比較常用的有Ksoap2,可以從http://code.google.com/p/ksoap2-android/downloads/list進行下載;將下載的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包復制到Eclipse工程的lib目錄中,當然也可以放在其他的目錄里。同時在Eclipse工程中引用這個jar包。
具體調用調用webservice的方法為:
(1) 指定webservice的命名空間和調用的方法名,如:
SoapObject request =new SoapObject(http://service,」getName」);
SoapObject類的第一個參數表示WebService的命名空間,可以從WSDL文檔中找到WebService的命名空間。第二個參數表示要調用的WebService方法名。
(2) 設置調用方法的參數值,如果沒有參數,可以省略,設置方法的參數值的代碼如下:
Request.addProperty(「param1」,」value」);
Request.addProperty(「param2」,」value」);
要注意的是,addProperty方法的第1個參數雖然表示調用方法的參數名,但該參數值並不一定與服務端的WebService類中的方法參數名一致,只要設置參數的順序一致即可。
(3) 生成調用Webservice方法的SOAP請求信息。該信息由SoapSerializationEnvelope對象描述,代碼為:
SoapSerializationEnvelope envelope=new
SoapSerializationEnvelope(SoapEnvelope.VER11);
Envelope.bodyOut = request;
創建SoapSerializationEnvelope對象時需要通過SoapSerializationEnvelope類的構造方法設置SOAP協議的版本號。該版本號需要根據服務端WebService的版本號設置。在創建SoapSerializationEnvelope對象後,不要忘了設置SOAPSoapSerializationEnvelope類的bodyOut屬性,該屬性的值就是在第一步創建的SoapObject對象。
(4) 創建HttpTransportsSE對象。通過HttpTransportsSE類的構造方法可以指定WebService的WSDL文檔的URL:
HttpTransportSE ht=new HttpTransportSE(「http://192.168.18.17:80
/axis2/service/SearchNewsService?wsdl」);
(5)使用call方法調用WebService方法,代碼:
ht.call(null,envelope);
Call方法的第一個參數一般為null,第2個參數就是在第3步創建的SoapSerializationEnvelope對象。
(6)使用getResponse方法獲得WebService方法的返回結果,代碼:
SoapObject soapObject =( SoapObject) envelope.getResponse();
以下為簡單的實現一個天氣查看功能的例子:

publicclass WebService extends Activity {
privatestaticfinal String NAMESPACE ="http://WebXml.com.cn/";
// WebService地址
privatestatic String URL ="http://www.webxml.com.cn/
webservices/weatherwebservice.asmx";
privatestaticfinal String METHOD_NAME ="getWeatherbyCityName";
privatestatic String SOAP_ACTION ="http://WebXml.com.cn/
getWeatherbyCityName";

private String weatherToday;

private Button okButton;
private SoapObject detail;

@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.ok);

okButton.setOnClickListener(new Button.OnClickListener() {
publicvoid onClick(View v) {
showWeather();
}
});
}

privatevoid showWeather() {
String city ="武漢";
getWeather(city);
}

@SuppressWarnings("deprecation")
publicvoid getWeather(String cityName) {
try {
System.out.println("rpc------");
SoapObject rpc =new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("rpc"+ rpc);
System.out.println("cityName is "+ cityName);
rpc.addProperty("theCityName", cityName);

AndroidHttpTransport ht =new AndroidHttpTransport(URL);
ht.debug =true;

SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.bodyOut = rpc;
envelope.dotNet =true;
envelope.setOutputSoapObject(rpc);

ht.call(SOAP_ACTION, envelope);

SoapObject result = (SoapObject) envelope.bodyIn;
detail = (SoapObject) result
.getProperty("getWeatherbyCityNameResult");

System.out.println("result"+ result);
System.out.println("detail"+ detail);
Toast.makeText(WebService.this, detail.toString(),
Toast.LENGTH_LONG).show();
parseWeather(detail);

return;
} catch (Exception e) {
e.printStackTrace();
}
}

privatevoid parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday ="今天:"+ date.split("")[0];
weatherToday = weatherToday +"\n天氣:"+ date.split("")[1];
weatherToday = weatherToday +"\n氣溫:"
+ detail.getProperty(5).toString();
weatherToday = weatherToday +"\n風力:"
+ detail.getProperty(7).toString() +"\n";
System.out.println("weatherToday is "+ weatherToday);
Toast.makeText(WebService.this, weatherToday,
Toast.LENGTH_LONG).show();

}
}