Hi i am trying to build an application on android which retrives data
from a web server, im using my localhost for the webservices. i have
developed jsp page to generate the data and pass to the android app.
the data is in xml format.

in the andorid app i keep getting a socket exception while trying to
open http connection using local host and the url includes jsp file
and http post parameters. i have set the permission in the manifest
file, still no luck.

below are my codes, im sure im missing something somewhere.

Can any1 assist me please.

Service
Puller::-----------------------------------------------------------------------------------------------------------------------------------------------
public class ServicePuller implements Runnable {


        private String pageInfo = "";
        private String serverUrl = "";
        private boolean dataReady = false;

        public void run() {
                dataReady = false;
                getInfo();
                dataReady = true;
        }

        public boolean isDataReady(){
                return dataReady;
        }

        public void setURL(String url){
                serverUrl = url;
        }

    public synchronized void getInfo(){
        clearData();
        try{
                InputStream is = OpenHttpConnection(serverUrl);
                InputStreamReader ir = new InputStreamReader(is);
                BufferedReader in = new BufferedReader(ir);
                String inputLine;
                while ((inputLine = in.readLine()) != null){
                        pageInfo += inputLine;
                }
                in.close();
                System.out.println("Page we got from url:" + serverUrl + " is
as below:");
                System.out.println(pageInfo);
        }
        catch(Exception ex){
            System.out.println("Exception caught, exception is: " +
ex.getMessage());
        }
    }

    private InputStream OpenHttpConnection(String urlString)
    throws IOException
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream
();
            }
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");
        }
        return in;
    }

    public synchronized void clearData(){
        pageInfo = "";
    }

    public synchronized String getPage(){
        return pageInfo;
    }
}

Service
Finder----------------------------------------------------------------------------------------------------------------------------------

public class ServiceFinder extends Activity{

        private string string;

        public ServiceFinder() {
    }
    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url = "http://localhost:13103/MobileWebApp/
MobiServices.jsp?locationID=1";
        //String url = "http://newsrss.bbc.co.uk/rss/
newsonline_uk_edition/front_page/rss.xml";
        setContentView(R.layout.service_finder);
        TextView tv = new TextView(this);
        ServicePuller sp = new ServicePuller();
        sp.setURL(url);
        Thread t = new Thread(sp);
        t.start();
        while(!sp.isDataReady()){
        try{
                Thread.sleep(5);
                }
                catch(Exception e){
                        System.out.println("Exception caught, exception is: " +
e.getMessage());
                }
        }
        String page = sp.getPage();
        System.out.println(page);
        tv.setText(page);
        setContentView(tv);
    }

manifest.xml----------------------------------------------------------------------------------------------------------------------------------------------

<manifest xmlns:android="http://schemas.android.com/apk/res/android";
package="com.servicefinder">
        <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="Service, Finder!">
        <activity android:name="ServiceFinder">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></
action>
                <category
android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>
    </application>
</manifest>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to