Solved

Implementing Yahoo finance onto android app

Posted on 2011-09-12
9
691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36527144
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
ID: 36527150
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
ID: 36527355
You can try kSOAP. I've used it to invoke a web service from an android app.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:lulubell-b
ID: 36540629
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36540664

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
ID: 36541602
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
ID: 36546963
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
ID: 36547758
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
ID: 36554176
Thank you, this is perfect.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 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