Asynctask for web service using ksoap giving exceptions

无人久伴 提交于 2019-12-11 10:22:26

问题


I am trying to run to Android code using an AsyncTask . However it is resulting in many exceptions. I am not able to remove these exceptions. The application stops unexpectedly.

Can someone help me ?

Here is my Android code :

     package com.example;
     import java.net.SocketException;
     import org.ksoap2.SoapEnvelope;
     import org.ksoap2.serialization.SoapObject;
     import org.ksoap2.serialization.SoapPrimitive;
     import org.ksoap2.serialization.SoapSerializationEnvelope; 
     import org.ksoap2.transport.HttpTransportSE;
     import android.app.Activity;
     import android.app.ProgressDialog;
     import android.os.AsyncTask;
     import android.os.Bundle; 
     import android.util.Log;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.Button;
     import android.widget.EditText;

   public class TeacherLoginActivity extends Activity
    {
    private final String NAMESPACE = "http://tempuri.org/";
    private final String URL = "http://10.0.2.2/loginteacher/Service1.asmx";

    String t_id;
    String password;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button signin = (Button) findViewById(R.id.regsubmitbtn);

            signin.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {


                            EditText etxt_user = (EditText) findViewById(R.id.usereditlog);
                            t_id = etxt_user.getText().toString();
                            EditText etxt_password = (EditText) findViewById(R.id.pwdeditlog);
                            password = etxt_password.getText().toString();


                            new LoginTask().execute();

                    }
            });   
        }

    private String doLogin(String t_id, String password) {

       String res=null;
       final String SOAP_ACTION = "http://tempuri.org/GetLogin";
       final String METHOD_NAME = "GetLogin";     
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

       request.addProperty("teacherid", t_id);
       request.addProperty("password",password);
       SoapSerializationEnvelope envelope = new    SoapSerializationEnvelope(SoapEnvelope.VER11);
       envelope.dotNet = true; // Set this variable to true for
                                                               // compatibility with what seems to be the
                                                               // default encoding for .Net-Services.
       envelope.setOutputSoapObject(request);

       System.out.println(request);

       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


       try {
              androidHttpTransport.call(SOAP_ACTION, envelope);
              SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
              Log.i("myApp", response.toString());
              System.out.println("response" +response);
              res=response.toString(); 
              System.out.println(res);


       }catch(SocketException ex)
       {
           Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
           ex.printStackTrace();
       }
       catch (Exception e) {
           Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
               e.printStackTrace();
       }

       return res;

       }


    private class LoginTask extends AsyncTask<Void, Void, Void> {

        private final ProgressDialog dialog = new ProgressDialog(
                TeacherLoginActivity.this);

        protected void onPreExecute() {

                this.dialog.setMessage("Logging in...");
                this.dialog.show();

        }


        protected Void doInBackground(final Void... unused) {

            String auth=doLogin(t_id,password);
            System.out.println(auth);

            return null; // don't interact with the ui!
        }


        protected void onPostExecute(Void result) 
        {

                if (this.dialog.isShowing()) {
                this.dialog.dismiss();
                                             }  

         }

      }

       }

My logcat : 

   10-27 20:03:11.329: ERROR/Error :(493): Error on soapPrimitiveData()  Permission denied
   10-27 20:14:31.458: ERROR/Error :(576): Error on soapPrimitiveData() Permission denied
   10-27 20:14:31.458: WARN/System.err(576): java.net.SocketException: Permission denied
   10-27 20:14:31.498: WARN/System.err(576):     at       org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
   10-27 20:14:31.498: WARN/System.err(576):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
   10-27 20:14:31.498: WARN/System.err(576):     at  org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265) 
   10-27 20:14:31.498: WARN/System.err(576):     at  java.net.Socket.checkClosedAndCreate(Socket.java:873)
   10-27 20:14:31.498: WARN/System.err(576):     at java.net.Socket.connect(Socket.java:1020)
   10-27 20:14:31.498: WARN/System.err(576):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
   10-27 20:14:31.498: WARN/System.err(576):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)

    10-27 20:14:32.869: ERROR/WindowManager(576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    10-27 20:14:32.869: ERROR/WindowManager(576):     at com.and roid.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    10-27 20:14:32.869: ERROR/WindowManager(576):     at dalvik.system.NativeStart.main(Native Method)
    10-27 20:14:34.188: INFO/Process(576): Sending signal. PID: 576 SIG: 9

回答1:


You probably need to add permissions in your XML file.

Something like:

<uses-permission
        android:name="android.permission.INTERNET" />


来源:https://stackoverflow.com/questions/7917751/asynctask-for-web-service-using-ksoap-giving-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!