问题
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.
回答1:
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
回答2:
@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...
回答3:
try to use my class
package com.example.xmlparser;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.util.Log;
import com.example.helpers.Helper;
public class SoapParser {
private static final String TAG = "SaopParser";
public String call(String name) {
String returnString = "";
SoapObject request = new SoapObject(Helper.TARGET_NAMESPACE,
Helper.OPERATION_NAME);
Log.i(TAG, "Getting Name :: " + name);
// request.addProperty("Celsius", txtCel.getText().toString());
request.addProperty("companyName", name);
// PropertyInfo info = new PropertyInfo();
//
// info.setType(PropertyInfo.STRING_CLASS);
// info.setName("companyName");
// info.setValue(name);
//
// request.addProperty(info);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
HttpTransportSE transportSE = new HttpTransportSE(
Helper.SOAP_ADDRESS);
transportSE.call(Helper.SOAP_ACTION, envelope);
returnString = envelope.getResponse().toString();
Log.i(TAG, envelope.getResponse().toString());
} catch (SoapFault e) {
e.printStackTrace();
Log.i(TAG, "Getting Exception");
} catch (Exception e) {
e.printStackTrace();
}
return returnString;
}
}
Helper.TARGET_NAMESPACE is namespace
Helper.OPERATION_NAME is mwthod name
you can take Refrece for here , here and [here].3
回答4:
I would of commented but I don't have enought rep.
I would try replacing
Object objectResult = (Object)envelope.getResponse();
with
SoapObject objectResult = (SoapObject)envelope.getResponse();
Also I noted that the webservice your pointing to is .aspx but you have envelope.dotNet = false; // Don't you want to change this to true?
来源:https://stackoverflow.com/questions/21979805/calling-asmx-from-android