標題:
WebService
[打印本頁]
作者:
ray
時間:
2012-11-16 18:48
標題:
WebService
http://ray.myweb999.com/download/ksoap2.jar
作者:
ray
時間:
2012-11-16 19:04
http://www.webserviceX.NET/
http://www.webservicex.net/stockquote.asmx
作者:
ray
時間:
2012-11-16 19:15
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25px"
android:text="美股價格 Web Service"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="股票代碼:" />
<EditText
android:id="@+id/symbleTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<Button
android:id="@+id/getBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取價" />
<TextView
android:id="@+id/resultTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-16 19:48
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View btn = this.findViewById(R.id.getBtn);
btn.setOnClickListener(this);
symbleTxt = (EditText)this.findViewById(R.id.symbleTxt);
resultTxt = (TextView)this.findViewById(R.id.resultTxt);
}
複製代碼
作者:
ray
時間:
2012-11-16 20:02
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v.getId() == R.id.getBtn)
{
resultTxt.setText(getPrice(symbleTxt.getText().toString()));
}
}
複製代碼
作者:
ray
時間:
2012-11-16 20:21
本帖最後由 ray 於 2012-11-16 20:24 編輯
protected String getPrice(String ss)
{
String info = "";
SoapObject request = new SoapObject(NAMESPACE,METHOD);
PropertyInfo symbleProp = new PropertyInfo();
symbleProp.setName("symbol");
symbleProp.setValue(ss);
symbleProp.setType(String.class);
request.addProperty(symbleProp);
return info;
}
複製代碼
作者:
ray
時間:
2012-11-16 20:26
package com.example.mywebservice;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener
{
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/stockquote.asmx";
private final String METHOD = "GetQuote";
EditText symbolTxt;
TextView resultTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View btn = this.findViewById(R.id.getBtn);
btn.setOnClickListener(this);
symbolTxt = (EditText)this.findViewById(R.id.symbolTxt);
resultTxt = (TextView)this.findViewById(R.id.resultTxt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v.getId() == R.id.getBtn)
{
resultTxt.setText(getPrice(symbolTxt.getText().toString()));
}
}
protected String getPrice(String ss)
{
String info = "";
SoapObject request = new SoapObject(NAMESPACE,METHOD);
PropertyInfo symbolProp = new PropertyInfo();
symbolProp.setName("symbol");
symbolProp.setValue(ss);
symbolProp.setType(String.class);
request.addProperty(symbolProp);
return info;
}
}
複製代碼
作者:
ray
時間:
2012-11-16 20:26
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25px"
android:text="美股價格 Web Service"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="股票代碼:" />
<EditText
android:id="@+id/symbolTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<Button
android:id="@+id/getBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取價" />
<TextView
android:id="@+id/resultTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-16 20:33
SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelop.dotNet = true;
envelop.setOutputSoapObject(request);
複製代碼
作者:
ray
時間:
2012-11-16 20:58
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
String res = response.toString();
複製代碼
作者:
ray
時間:
2012-11-16 21:16
String parseXML(String xml,String pattern)
{
String start = "<"+pattern+">";
String end = "</"+pattern+">";
return xml.substring(xml.indexOf(start)+start.length(),xml.indexOf(end));
}
複製代碼
作者:
ray
時間:
2012-11-16 21:18
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
複製代碼
作者:
ray
時間:
2012-11-16 21:31
info = "公司名稱:" + parseXML(res,"Name") + "\n";
info += "價格時間:" + parseXML(res,"Date") + " " + parseXML(res,"Time") + "\n";
info += "目前價格:" + parseXML(res,"Last") + "\n";
info += "昨天收盤:" + parseXML(res,"PreviousClose") + "\n";
info += "今天開盤:" + parseXML(res,"Open") + "\n";
info += "今天最高:" + parseXML(res,"High") + "\n";
info += "今天最低:" + parseXML(res,"Low") + "\n";
info += "張跌金額:" + parseXML(res,"Change") + "\n";
info += "張跌比率:" + parseXML(res,"PercentageChange") + "\n";
複製代碼
作者:
ray
時間:
2012-11-19 18:35
[attach]632[/attach]
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2