Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
 
I have an ASP.NET web service hosted locally on my computer, and I am trying to get an Android application to access it.
 
This is the code:
 
<small></small> package com.example.projectmanager;
    
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.*;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    
    public class MainActivity extends Activity {
    	
    	private EditText username, password;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            
          
    	  username = (EditText) findViewById(R.id.txtUsername);
          password = (EditText) findViewById(R.id.txtPassword);
            
           
           
            Button loginButton = (Button)findViewById(R.id.btnLogin);
            loginButton.setOnClickListener((android.view.View.OnClickListener) mAddListener);
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
     
        public OnClickListener mAddListener = new OnClickListener(){
        	public void onClick(View v) {
        		
        		
        		
        		String  mUsername = username.getText().toString();
                String  mPassword = password.getText().toString();
    			
    			tryLogin(mUsername, mPassword);}
    
    		@Override
    		public void onClick(DialogInterface dialog, int which) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        };
        		
        		protected void tryLogin(String mUsername, String mPassword)
        	    {           
        	        HttpURLConnection connection;
        	       OutputStreamWriter request = null;
    
        	            URL url = null;   
        	            String response = null;         
        	            String parameters = "userName="+mUsername+"&password="+mPassword;   
    
        	            try
        	            {
        	                url = new URL("http://localhost:65249/ThesisApp/WebService.asmx?op=Login");
        	                connection = (HttpURLConnection) url.openConnection();
        	                connection.setDoOutput(true);
        	                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        	                connection.setRequestMethod("POST");    
    
        	                request = new OutputStreamWriter(connection.getOutputStream());
        	                request.write(parameters);
        	                request.flush();
        	                request.close();            
        	                String line = "";               
        	                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        	                BufferedReader reader = new BufferedReader(isr);
        	                StringBuilder sb = new StringBuilder();
        	                while ((line = reader.readLine()) != null)
        	                {
        	                    sb.append(line + "\n");
        	                }
        	                // Response from server after login process will be stored in response variable.                
        	                response = sb.toString();
        	                // You can perform UI operations here
        	                Toast.makeText(this,"Message from Server: \n"+ response, 0).show();             
        	                isr.close();
        	                reader.close();
    
        	            }
        	            catch(IOException e)
        	            {
        	                // Error
        	            }
        	    }
        		 
        	
    
    		public void onClick(DialogInterface dialog, int which) {
    			// TODO Auto-generated method stub
    			
    		}
    
    	
        
    }
 
Whenever I try to run it I get this error:
 
    04-22 01:30:09.413: E/Trace(1358): error opening trace file: No such file or directory (2)
    04-22 01:30:19.869: D/AndroidRuntime(1358): Shutting down VM
    04-22 01:30:19.869: W/dalvikvm(1358): threadid=1: thread exiting with uncaught exception (group=0xb4fd5288)
    04-22 01:30:19.919: E/AndroidRuntime(1358): FATAL EXCEPTION: main
    04-22 01:30:19.919: E/AndroidRuntime(1358): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectmanager/com.example.projectmanager.MainActivity}: java.lang.ClassCastException: com.example.projectmanager.MainActivity$1 cannot be cast to android.view.View$OnClickListener
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.ActivityThread.access$600(ActivityThread.java:130)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.os.Looper.loop(Looper.java:137)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at java.lang.reflect.Method.invokeNative(Native Method)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at java.lang.reflect.Method.invoke(Method.java:511)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at dalvik.system.NativeStart.main(Native Method)
    04-22 01:30:19.919: E/AndroidRuntime(1358): Caused by: java.lang.ClassCastException: com.example.projectmanager.MainActivity$1 cannot be cast to android.view.View$OnClickListener
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at com.example.projectmanager.MainActivity.onCreate(MainActivity.java:50)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.Activity.performCreate(Activity.java:5008)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    04-22 01:30:19.919: E/AndroidRuntime(1358): 	... 11 more
 

 
And the android application crashes.
 
Can anyone tell me what's going on?
Posted 21-Apr-13 16:36pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100