Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Asmx From Android

I Am Trying To call Asmx (.net Web Services) From Android Application. But When I am giving data It is Giving Error :

AndroidRuntime(591): FATAL EXCEPTION: Thread-75
AndroidRuntime(591): java.lang.NullPointerException: println needs a message

in logcat showing data is going:

D\Req value1(540): NetPositionReport{arg0=64396; b=Om$@!#@M^#R; }

What Could Be The Problem?

MainActivity.java

package com.example.clientnetpositionreport;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
   EditText e1;
   Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1=(Button) findViewById(R.id.btn);
        b1.setOnClickListener(new OnClickListener(){
            public void onClick(View v){

                e1=(EditText)findViewById(R.id.evofclientcode);

               Intent i=new Intent(MainActivity.this,GetReport.class);
               i.putExtra("ClientCode",e1.getText().toString());
               startActivity(i);
            }
        });
    }

    @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;
    }

}

GetReport.java

package com.example.clientnetpositionreport;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;



import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class GetReport extends Activity{
    private static final String NAMESPACE ="http://xxxxxxx
    private static final String URL = "http://xxxxxxxxxxxx/services/xxxx.asmx"; 
    private static final String METHOD_NAME = "xxxxxxx";
    private static final String SOAP_ACTION = "http://xxxx/xxxxxxxxx";
    private String webResponse = "";
    private Handler handler = new Handler();       
    private Thread thread;

    private TextView textView1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(1);
        setContentView(R.layout.activity_tv);
        textView1 = (TextView) findViewById(R.id.textview1);

        //int val=getIntent().getExtras().getInt("Title");
        String title= getIntent().getExtras().getString("ClientCode");
        //Log.d("Data is", val);

        System.out.println("Data is "+title);

        startWebAccess(title);

}
    public void startWebAccess(String a){
        final String aa=a;
         thread = new Thread(){
               public void run(){
                try{
                     Log.d("Req value0R", "Starting...");//log.d is used for debug
                  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                  //simple object access protocol
                  PropertyInfo fromProp =new PropertyInfo();
                  fromProp.setName("arg0");
                  fromProp.setValue(aa);
                  fromProp.setType(String.class);
                  request.addProperty(fromProp); 

              PropertyInfo fromProp2 =new PropertyInfo();
                 fromProp2.setName("b");
                  fromProp2.setValue("Om$@!#@M^#R");
                  fromProp2.setType(String.class);
                  request.addProperty(fromProp2); 

                     Log.d("Req value1", request.toString());

                     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                   envelope.dotNet = false;
                     envelope.setOutputSoapObject(request);

                     Log.d("Req value2", envelope.toString());
                     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                     System.out.println("problem1");
                     androidHttpTransport.debug = true;
                     System.out.println("problem2");
                     Log.d("Req value2B", androidHttpTransport.toString());
                     System.out.println("problem3");
                     androidHttpTransport.call(SOAP_ACTION, envelope);
                     System.out.println("problem4");
                     Log.d("Req value2C", androidHttpTransport.toString());
                     System.out.println("problem5");
                     Object objectResult = (Object)envelope.getResponse();
                     System.out.println("problem6");
                     webResponse = objectResult.toString();
                     System.out.println("problem7");

                }

                catch(Exception e){
                      System.out.println("problem8");
                    Log.d("Req value4", e.getMessage() );    
                    webResponse = "Connection/Internet problem";
                    webResponse = "Connection/Internet problem";
               //  Toast.makeText(getApplicationContext(), "Loading problem/Server down", Toast.LENGTH_SHORT).show();
                }

                handler.post(createUI);
               }
              };

              thread.start();
             }


             final Runnable createUI = new Runnable() {

              public void run(){
                  if(webResponse!=null) {

              textView1.setText(webResponse);
                  }
                  else {
                      webResponse ="No data provided presently";
                      textView1.setText(webResponse);
                  }
              }
              };

    }

note: The console output is till problem6. That means the value is Null (What I think.).So please keep that in mind.

Activity_Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <TextView 
       android:id="@+id/tvofclientcode"
       android:layout_width="100dp"
       android:layout_height="30dp"
       android:text="Client Code"
       />
   <EditText 
      android:id="@+id/evofclientcode"
      android:layout_width="100dp"
      android:layout_height="50dp"
      android:layout_toRightOf="@+id/tvofclientcode"
      android:inputType="text"/>
   <Button
       android:id="@+id/btn"
       android:layout_width="100dp"
       android:layout_height="40dp"
       android:layout_below="@+id/tvofclientcode"
       android:text="Submit"/>
</RelativeLayout>

What Could Be The Problem?

Note: the log.d method is getting string values,so that isn't the problem as was in some related cases.

like image 696
Ravi sinha Avatar asked Dec 20 '22 17:12

Ravi sinha


2 Answers

@xxeemez-ashu:

I get response, change your GetReport.java with below code

public class GetReport extends Activity {

private static final String NAMESPACE = "http://tempuri.org/"; // com.service.ServiceImpl
private static final String URL = "http://commodities.karvy.com/services/NetPositionReport.asmx";
private static final String METHOD_NAME = "NetPositionReport";
private static final String SOAP_ACTION = "http://tempuri.org/NetPositionReport";
private String webResponse = "";
private Handler handler = new Handler();
private Thread thread;

private TextView textView1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(1);
    setContentView(R.layout.activity_tv);
    textView1 = (TextView) findViewById(R.id.textView1);

    String title = getIntent().getExtras().getString("ClientCode");

    startWebAccess(title);

}

public void startWebAccess(String a) {
    final String aa = a;
    thread = new Thread() {
        public void run() {
            try {
                Log.d("Req value0R", "Starting...");// log.d is used for
                                                    // debug
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                request.addProperty("ClientCode", "64396");
                request.addProperty("key", "Om$@!#@M^#R");

                Log.d("Req value1", request.toString());

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);

                HttpTransportSE androidHttpTransport = new HttpTransportSE(
                        URL);
                androidHttpTransport.debug = true;
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject objectResult = (SoapObject) envelope.bodyIn;
                webResponse = objectResult.toString();
                System.out.println("response: " + webResponse);

            } catch (SoapFault sp) {

                sp.getMessage();
                System.out.println("error = " + sp.getMessage());

            } catch (Exception e) {
                System.out.println("problem8");
                e.printStackTrace();

                webResponse = "Connection/Internet problem";
            }

            handler.post(createUI);
        }
    };

    thread.start();
}

final Runnable createUI = new Runnable() {

    public void run() {
        if (webResponse != null) {

            textView1.setText(webResponse);
        } else {
            webResponse = "No data provided presently";
            textView1.setText(webResponse);
        }
    }
};

}

save changes and run the application and see the logcat, you will get the data there and enjoy...

let me know if any issue, and please select my answer if its working properly...

like image 45
Jayesh Avatar answered Dec 28 '22 12:12

Jayesh


After read your code I found out many minor mistakes..

I can't write all mistake here so just compare your code with mine code and understood your mistakes.

This is your Asmx Web-Service.

Here you have to pass two input paramater

 <ClientCode>string</ClientCode>
 <key>string</key>

but you are passing everything wrong..

Your Wrong Code

PropertyInfo fromProp =new PropertyInfo();
fromProp.setName("arg0");
fromProp.setValue(aa);
fromProp.setType(String.class);
request.addProperty(fromProp); 

PropertyInfo fromProp2 =new PropertyInfo();
fromProp2.setName("b");
fromProp2.setValue("Om$@!#@M^#R");
fromProp2.setType(String.class);
request.addProperty(fromProp2); 

You have to pass like this

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    // simple object access protocol
request.addProperty("ClientCode", aa);
request.addProperty("key", "Om$@!#@M^#R");

Working Code of GetReport.Class

package com.example.clientnetpositionreport;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class GetReport extends Activity {
    private static final String NAMESPACE = "http://tempuri.org/"; // com.service.ServiceImpl
    private static final String URL = "http://commodities.karvy.com/services/NetPositionReport.asmx";
    private static final String METHOD_NAME = "NetPositionReport";
    private static final String SOAP_ACTION = "http://tempuri.org/NetPositionReport";
    private String webResponse = "";
    private Handler handler = new Handler();
    private Thread thread;

    private TextView textView1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(1);
        setContentView(R.layout.fragment_item_detail);
        textView1 = (TextView) findViewById(R.id.item_detail);

        String title= getIntent().getExtras().getString("ClientCode");

        System.out.println("Data is " + title);

        startWebAccess(title);

    }

    public void startWebAccess(String a) {
        final String aa = a;
        thread = new Thread() {
            public void run() {
                try {
                    Log.d("Req value0R", "Starting...");
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    // simple object access protocol
                    request.addProperty("ClientCode", aa);
                    request.addProperty("key", "Om$@!#@M^#R");

                    Log.d("Req value1", request.toString());

                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
                    envelope.dotNet = true;
                    envelope.bodyOut = request;
                    // envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);

                    Log.d("Req value2", envelope.toString());

                    androidHttpTransport.debug = true;
                    androidHttpTransport.call(SOAP_ACTION, envelope);

                    SoapObject resultData = (SoapObject) envelope.getResponse();
                    if (resultData != null)
                        webResponse = resultData.toString();
                    else
                        webResponse = "No Data found.";
                    System.out.println("webResponse : " + webResponse);

                }

                catch (Exception e) {
                    e.printStackTrace();
                    webResponse = "Connection/Internet problem";
                }
                handler.post(createUI);
            }
        };
        thread.start();
    }

    final Runnable createUI = new Runnable() {

        public void run() {
            if (webResponse != null) {
                textView1.setText(webResponse);
            } else {
                webResponse = "No data provided presently";
                textView1.setText(webResponse);
            }
        }
    };

}

This is code is working but I am not getting response may be I am passing wrong value of ClientCode and key

like image 119
Niranj Patel Avatar answered Dec 28 '22 12:12

Niranj Patel