Java – Call Asmx from Android

Call Asmx from Android… here is a solution to the problem.

Call Asmx from Android

I’m trying to call Asmx (.net Web Service) from an Android application. But I get an error when I provide the data:

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

In logcat where data is displayed:

D\Req value1(540): NetPositionReport{arg0=64396; [email protected]!#@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("[email protected]!#@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 a string value, so this is not an issue in some related cases.

Solution

After reading your code I found a lot of small bugs…

I can’t write all the errors here, so just compare your code to mine and understand your errors.

This is your Asmx Web-Service.

Here you have to pass two input parameters

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

But you passed everything wrong…

Your error 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("[email protected]!#@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", "[email protected]!#@M^#R");

The working code for 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", "[email protected]!#@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 the code, but I’m not getting a response, probably I passed the wrong ClientCode and key

values

Related Problems and Solutions