使用HTTP Post连接到ASP.NE Web服务(ANDROID) [英] Connecting to a ASP.NE Web Service with HTTP Post (ANDROID)

查看:52
本文介绍了使用HTTP Post连接到ASP.NE Web服务(ANDROID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的计算机本地托管了一个ASP.NET Web服务,我正在尝试使用Android应用程序来访问它。 />


这是代码:



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?

推荐答案

1无法转换为android.view.View
1 cannot be cast to android.view.View


OnClickListener
04-22 01:30: 19.919:E / AndroidRuntime(1358):在android.app.ActivityThread.performLaunchActivity(Activ) ityThread.java:2059)
04-22 01:30:19.919:E / AndroidRuntime(1358):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-22 01: 30:19.919:E / AndroidRuntime(1358):在android.app.ActivityThread.access
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):在android.app.ActivityThread
600(ActivityThread.java:130) 04-22 01:30:19.919: E/AndroidRuntime(1358): at android.app.ActivityThread


这篇关于使用HTTP Post连接到ASP.NE Web服务(ANDROID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆