Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

Implementing Yahoo finance onto android app

Hello,

I want to implement yahoo finance into my android application. Please help. I want to enter ticker symbol and its retrieves stock price and related information.

Thanks

0
lulubell-b
Asked:
lulubell-b
  • 4
  • 3
  • 2
1 Solution
 
for_yanCommented:
Did you try to do it through the system browser?
At least I know that on blackberry 9700 it works pretty nicely.
0
 
lulubell-bAuthor Commented:
No I haven't. I want to be able to do thru a web service. I want to feed the ticker number thru the service and return the corresponding into variables into java.
0
 
ariohCommented:
You can try kSOAP. I've used it to invoke a web service from an android app.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lulubell-bAuthor Commented:
I need some more help. I was able to download KSOAP2, now I need some help implementing a web service.

service is http://www.webservicex.net/stockquote.asmx?WSDL

I want to enter stock symbol and want it to return the stock price.

Need some help implementing code from

http://java.dzone.com/articles/invoke-webservices-android


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:weightSum="1">
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical">
        <EditText android:id="@+id/txtStockSym" android:layout_height="66dp" android:layout_width="265dp" android:layout_gravity="right">
            <requestFocus></requestFocus>
        </EditText>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout3" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:layout_weight="0.44" android:layout_gravity="bottom">
        <Button android:text="Enter" android:id="@+id/btnRequest" android:layout_height="wrap_content" android:layout_width="132dp" android:layout_gravity="center"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout4" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"></LinearLayout>
    <LinearLayout android:orientation="vertical" android:id="@+id/linearLayout2" android:layout_weight="0.12" android:layout_height="wrap_content" android:layout_width="match_parent">
        <TextView android:id="@+id/txtOutput" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
    </LinearLayout>
    
</LinearLayout>


package com.CSV.Buescher;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class StockQuoteLkup extends Activity {
	 
	private static final String SOAP_ACTION = "http://www.naveenbalani.com/webservices/WassupAndroidService/todaysMessage";
	 
	private static final String OPERATION_NAME = "todaysMessage";
	 
	private static final String WSDL_TARGET_NAMESPACE = "http://www.naveenbalani.com/webservices/WassupAndroidService/";
	 
	private static final String SOAP_ADDRESS = "http://naveenbalani.com/WassupAndroid.asmx";
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	 
	TextView textView = new TextView(this);
	 
	setContentView(textView);
	 
	SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
	OPERATION_NAME);
	 
	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	SoapEnvelope.VER11);
	envelope.dotNet = true;
	 
	envelope.setOutputSoapObject(request);
	 
	HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
	 
	try
	 
	{
	 
	httpTransport.call(SOAP_ACTION, envelope);
	 
	Object response = envelope.getResponse();
	 
	textView.setText(response.toString());
	 
	}
	 
	catch (Exception exception)
	 
	{
	 
	textView.setText(exception.toString());
	 
	}
	 
	}
}

Open in new window

0
 
for_yanCommented:

The simplest way to get stock price  is to use HTTP connection and parse very simple XML which they return from this URL
http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=bac
You don't need even to use web service for this purpose.
0
 
ariohCommented:
can't test it on android right now, but should work
package testksoap;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class TestYahoo {

  private static final String NAMESPACE = "http://www.webserviceX.NET/";
  private static final String SERVICE_URL = "http://www.webservicex.net/stockquote.asmx";
  private static final String METHOD_NAME = "GetQuote";
  private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;

  public static void main( String[] args ) {
    SoapObject request = new SoapObject( NAMESPACE, METHOD_NAME );
    request.addProperty( "symbol", "BAC" );
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11 );
    envelope.setOutputSoapObject( request );
    envelope.dotNet = true;
    HttpTransportSE httpTransport = new HttpTransportSE( SERVICE_URL );
    //httpTransport.debug = true;
    try {
      httpTransport.call( SOAP_ACTION, envelope );
      SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
//      System.out.println( result.toString() );
      parseResponse( result.toString() );
    } catch ( Exception e ) {
      e.printStackTrace( System.err );
    }
  }
  
  private static void parseResponse( String response ) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse( new InputSource( new InputStreamReader( new ByteArrayInputStream( response.getBytes() ), "UTF-8" ) ) );
    Element element = document.getDocumentElement();
    NodeList stocks = element.getElementsByTagName( "Stock" );
    if ( stocks.getLength() > 0 ) {
      for ( int i = 0; i < stocks.getLength(); i++ ) {
        System.out.println( "--- Stock" );
        Element stock = (Element) stocks.item( i );
        printValue( stock, "Symbol" );
        printValue( stock, "Last" );
        printValue( stock, "Date" );
        printValue( stock, "Time" );
        printValue( stock, "Change" );
        printValue( stock, "Open" );
        printValue( stock, "High" );
        printValue( stock, "Low" );
        printValue( stock, "Volume" );
        printValue( stock, "MktCap" );
        printValue( stock, "PreviousClose" );
        printValue( stock, "PercentageChange" );
        printValue( stock, "AnnRange" );
        printValue( stock, "Earns" );
        printValue( stock, "P-E" );
        printValue( stock, "Name" );
      }
    }
  }
  
  private static void printValue( Element element, String name ) {
    Element value = (Element) element.getElementsByTagName( name ).item( 0 );
    System.out.println( name + ": " + value.getFirstChild().getNodeValue() );
  }
  
}

Open in new window

0
 
lulubell-bAuthor Commented:
So I change testYahoo to

public class StockQuoteLkup extends Activity { }

Thats ok? correct?

Next,

Which part is the input of the SYMBOL?

    request.addProperty( "symbol", "BAC" );

Would I change it to something like this?
myStock = (EditText)findViewById(R.id.txtStockPrice);

Is this the output?

System.out.println( name + ": " + value.getFirstChild().getNodeValue() );

Would I change it to something like this?

                                sPV = myPutValue.getText().toString();
                                dPV = Double.parseDouble(sPV);

I'm new to java programming and very new to android programming.

Thank you for the help
0
 
ariohCommented:
Yes, symbol is set by request.addProperty( "symbol", "BAC" );

Using the layout xml from your code it should be:
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class StockQuoteLkup extends Activity {
	
  private static final String NAMESPACE = "http://www.webserviceX.NET/";
  private static final String SERVICE_URL = "http://www.webservicex.net/stockquote.asmx";
  private static final String METHOD_NAME = "GetQuote";
  private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
	
  @Override
  public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.main );
    Button btnRequest = (Button) findViewById( R.id.btnRequest );
    btnRequest.setOnClickListener( new OnClickListener(){
      public void onClick( View view ) {
    	requestStock();
      }
    } );
  }
  
  private void requestStock() {
	EditText edSymbol = (EditText) findViewById( R.id.txtStockSym );
    SoapObject request = new SoapObject( NAMESPACE, METHOD_NAME );
    request.addProperty( "symbol", edSymbol.getText().toString() );
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11 );
    envelope.setOutputSoapObject( request );
    envelope.dotNet = true;
    HttpTransportSE httpTransport = new HttpTransportSE( SERVICE_URL );
    //httpTransport.debug = true;
    try {
      httpTransport.call( SOAP_ACTION, envelope );
      SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
      parseResponse( result.toString() );
    } catch ( Exception e ) {
      Log.d( "STOCK", e.getClass().getName() + ": " + e.getMessage() );
      Toast t = Toast.makeText( this, e.getClass().getName() + ": " + e.getMessage(), 10 );
      t.show();
    }
  }
  
  private void parseResponse( String response ) throws Exception {
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	DocumentBuilder db = dbf.newDocumentBuilder();
	Document document = db.parse( new InputSource( new InputStreamReader( new ByteArrayInputStream( response.getBytes() ), "UTF-8" ) ) );
	Element element = document.getDocumentElement();
	NodeList stocks = element.getElementsByTagName( "Stock" );
	if ( stocks.getLength() > 0 ) {
      for ( int i = 0; i < stocks.getLength(); i++ ) {
        Element stock = (Element) stocks.item( i );
        Element value = (Element) stock.getElementsByTagName( "Last" ).item( 0 );
        TextView tv = (TextView) findViewById( R.id.txtOutput );
        tv.setText( value.getFirstChild().getNodeValue() );
        break;
      }
    }
  }  
  
}

Open in new window

0
 
lulubell-bAuthor Commented:
Thank you, this is perfect.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now