Solved

Implementing Yahoo finance onto android app

Posted on 2011-09-12
9
674 Views
Last Modified: 2012-05-12
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
Comment
Question by:lulubell-b
  • 4
  • 3
  • 2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Did you try to do it through the system browser?
At least I know that on blackberry 9700 it works pretty nicely.
0
 

Author Comment

by:lulubell-b
Comment Utility
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
 
LVL 9

Expert Comment

by:arioh
Comment Utility
You can try kSOAP. I've used it to invoke a web service from an android app.
0
 

Author Comment

by:lulubell-b
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 47

Expert Comment

by:for_yan
Comment Utility

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
 
LVL 9

Expert Comment

by:arioh
Comment Utility
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
 

Author Comment

by:lulubell-b
Comment Utility
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
 
LVL 9

Accepted Solution

by:
arioh earned 500 total points
Comment Utility
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
 

Author Comment

by:lulubell-b
Comment Utility
Thank you, this is perfect.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now