how to communicate with a web server

by aamir » Tue, 14 Apr 2009 06:02:11 GMT


Sponsored Links
 Hi all,
            I would like to build a basic application that would
communicated with a web server running on my computer (  http://192.168.0.0.0 )
.My computer runs apache.Any help will be greatly appreciated.

    Aamir
--~--~---------~--~----~------------~-------~--~----~



how to communicate with a web server

by Arnaud Weber » Tue, 14 Apr 2009 06:14:14 GMT


 Emulator run on a VM so it has it's own network. your loopback 192.168.0.0
is not going to work.
There is a bridge between the network of your emulator and your own loopback
at the adress 10.0.2.2.

Try this address and that should work.

Hope it helps
Arnaudweb

2009/4/14 aamir <aamir.sye...@gmail.com>


--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. How to repeat Login dialog and progress dialog, coordinating with http thread

Hi there,

I am having a problem about repeating Login dialog (an AlertDialog)
and progress dialog, coordinating with http thread. I suppose
repetitive Login dialog (if fail, continue) handling should be common
and straightforward. I guess my approach must be wrong somewhere. I
already spent 2 days on this and am desperate. So please help.

The usecase is like this:

I have a main activity.

User starts the app, the main activity starts.
Show a login dialog (generated by the main thread, i.e. from onCreate
()). The main thread then starts a wait_thread, which will wait for
http to return data and check the data and decide what to do.

After user input username/password and press login, a progress dialog
starts.

The progress dialog starts an http_thread to talk to the server and
get replies. Once done, it will notify the waiting thread.

If the user type in the right username password first time, the code
works fine.

But it always fail for 2nd time Login, i.e. When first login fail
(wrong username/password), the wait_thread will generate 2nd Login
dialog to let user repeat the login process. But after user hit the
login on this 2nd Login dialog, the system always crashes.

The exception here is:

ERROR/AndroidRuntime(21880): Uncaught handler: thread main exiting due
to uncaught exception
ERROR/AndroidRuntime(21880): android.view.ViewRoot
$CalledFromWrongThreadException: Only the original thread that created
a view hierarchy can touch its views.

The followings are part of the code.
        ...
public class Test extends Activity implements Constants,Runnable{
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        showDialog(DIALOG_LOGIN); //
                startHttpWaitThread();
    }

    private void startHttpWaitThread(){
        try {
                Thread dialogThread  = new Thread(this);
                dialogThread.start();
                } catch(Exception e){
                //showDialog(DIALOG_LOGIN);
            }
    }
/*      @Override
        public void onStart() {
                super.onStart();
        } */

    public void run(){
        Looper.prepare();

                try {
                synchronized(httpReplyData){ // httpReplyData holds all needed
data returned from server
                        httpReplyData.wait();
                }
                } catch(Exception e) {
                        e.printStackTrace();
                }
                if 
(httpReplyData.getHttpResult()!=HTTP_REPLY_STATUS_CODE_OK_200)
showDialog(DIALOG_LOGIN);
        Looper.loop();
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_LOGIN:
                return getLoginDialog(this);//,
        case DIALOG_LOGIN_PROGRESS:
                return new LoginProgressDialog
(ctxForLoginProgressDialog);//        }
        return null;
    }

    private Dialog getLoginDialog(Context ctx) {//, String name,
String pwd){
        ...
                dialogBuilder.setPositiveButton(R.string.login, new
DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int
whichButton) {
                        networkService.startHttpThread
(HTTP_REQUEST_LOGIN_INDEX, getUsername(), getPassword()); //
networkService is the HTTP manager
                        showDialog(DIALOG_LOGIN_PROGRESS); //
                        startHttpWaitThread();
                }
            });
        return dialogBuilder.create();
    }

    private class LoginProgressDialog extends ProgressDialog
implements Constants{
        //private ProgressThread progressThread;
                public LoginProgressDialog(Context ctx){
                        super(ctx);
                        ...
                }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~
I also tried second approach, which moves the wait_thread into
progress dialog class/object, following Google reating a
ProgressDialog(http://developer.android.com/guide/topics/ui/
dialogs.html#ProgressDialog). The 2nd login dialog is started by the
progress dialog. Its wait() method gets called ONLY once (that is
before 3nd showDialog(DIALOG_LOGIN) gets called). Seems to me the
unfinished progressDialog thread blocks new progressDialog to be
generated, I mean, when new showDialog(DIALOG_LOGIN_PROGRESS) called,
the system will continue to use last progressDialog which has already
responded to the first notify call and won respond to the notify
call any more. So although the http thread handles well and call
notify in due time, no one is waiting for that notify. Thus the system
just hungs, while progress dialog is doing it animation endlessly.

The followings are part of the code:
        public class Test {// extends Activity implements Constants {
        ...
        @Override
    public void onCreate(Bundle savedInstanceState) {
                ...
        showDialog(DIALOG_LOGIN); //
    }

        ...
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_LOGIN:
                return getLoginDialog(this);//
        case DIALOG_LOGIN_PROGRESS:
                return new LoginProgressDialog(ctxForLoginProgressDialog);//
        }
        return null;
    }
    private Dialog getLoginDialog(Context ctx) {//, String name,
String pwd){
        ...
                dialogBuilder.setPositiveButton(R.string.login, new
DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int
whichButton) {
                        ...
                        networkService.startHttpThread(HTTP_REQUEST_LOGIN_INDEX,
username, password);
                        showDialog(DIALOG_LOGIN_PROGRESS); //
                }
            });
        return dialogBuilder.create();
    }

    private class LoginProgressDialog extends ProgressDialog
implements Constants{
        private ProgressThread progressThread;
                public LoginProgressDialog(Context ctx){
                        ...
                        progressThread = new ProgressThread(handler);
            progressThread.start();
                }
            final Handler handler = new Handler() {
                public void handleMessage(Message msg) {
                        int httpStatusCode = 
msg.getData().getInt("httpStatusCode");

                    if (httpStatusCode == HTTP_REPLY_STATUS_CODE_OK_200){
                        dismissDialog
(DIALOG_LOGIN_PROGRESS);
                    } else showDialog(DIALOG_LOGIN);
                }
            };
            private class ProgressThread extends Thread {
                Handler handler;
                ProgressThread(Handler handler){
                        this.handler = handler;
                }
                public void run() {
                        try {
                                synchronized(httpReplyData){
                                        httpReplyData.wait();
                                }
                        } catch(Exception e) {
                                e.printStackTrace();
                        }
                        Message msg = handler.obtainMessage();
                Bundle b = new Bundle();
                b.putInt("httpStatusCode", httpReplyData.getHttpResult
());
                msg.setData(b);
                handler.sendMessage(msg);
                }
            }
        }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How should I correct the logic? Any help would be truly appreciated

-- 

2. Browser Name?.

Hi,

what's the name of default browser in android?..
for iphone safari... for android ?..

-- 
Thanks & Regards
Sasikumar.S

-- 

3. Dev Labs in Barcelona

4. Proven miley vs nexus

5. Boot up directly to the google search:Redude first screen time

6. MotionEvent ACTION_DOWN throwing security exception for INJECT_EVENT permission

7. MotionEvent ACTION_DOWN throwing security exception for INJECT_EVENT permission