you are a legend david!!

2009/3/24 David Turner <di...@android.com>

> Connect to 10.0.2.2 which is the "magic" address in the emulator to connect
> to your host machine's localhost.
>
> See the emulator documentation for more details.
>
>
> On Tue, Mar 24, 2009 at 9:51 AM, talha.rahma...@googlemail.com <
> talha.rahma...@googlemail.com> wrote:
>
>>
>>
>> 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<http://localhost:13103/MobileWebApp/%0AMobiServices.jsp?locationID=1>
>> ";
>>        //String url = "http://newsrss.bbc.co.uk/rss/
>> newsonline_uk_edition/front_page/rss.xml<http://newsrss.bbc.co.uk/rss/%0Anewsonline_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>
>>
>>
>>
>
>  >
>


-- 
Talha Rahman

Mobile Computing Bsc
talha.rahma...@gmail.com
07731 985 305
Isaac Asimov  - "I do not fear computers. I fear the lack of them."

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

Reply via email to