Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Implementing Yahoo finance onto android app

Posted on 2011-09-12
9
Medium Priority
?
698 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
Industry Leaders: 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!

 

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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

688 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