I've programmed ( actually followed a tutorial ) simple web service in android, using Ksoap libary. But it seems that it doesn't work well. Below is my simple code:
public class MainActivity extends Activity {
TextView tv;
SoapPrimitive response;
final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
final String METHOD_NAME = "CelsiusToFahrenheit";
final String NAMESPACE = "http://www.tempuri.org/";
final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getValue();
}
});
}
private void getValue() {
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
soapObject.addProperty("Celsius", "33");
SoapSerializationEnvelope serial = new SoapSerializationEnvelope(SoapEnvelope.VER11);
serial.dotNet = true;
serial.setOutputSoapObject(soapObject);
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.call(SOAP_ACTION, serial);
response = (SoapPrimitive) serial.getResponse();
tv.setText(response+"");
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
I only get a response "Error" in my text view. Can anyone tell me what is wrong here?
I found what was wrong. I wrote:
final String NAMESPACE = "http://www.tempuri.org/";
instead of
final String NAMESPACE = "http://tempuri.org/";
It's working now.
Related
I'm trying to send a json String but when I call the WebService he send a null parameter instead of my string.
When I go to Debug I can see on the soapObject Propertis my json. but in my webService i've put and when I call from my andoid app he always return null
if (json.Equals(null)) {
return "null";
}
try {
return json;
root = JObject.Parse(json);
} catch (Exception e) {
return e.StackTrace;
}
return "parseok";
Here is the code that I'm using.
public class OpcoesActivity extends Activity implements OnClickListener {
private String cpf;
private String senha;
private PontosUsuarioDAO pdao = new PontosUsuarioDAO(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.opcoeslayout);
cpf = getIntent().getStringExtra("cpf");
senha = getIntent().getStringExtra("senha");
Button importar = (Button) findViewById(R.id.bt_importar);
importar.setOnClickListener(this);
Button exportar = (Button) findViewById(R.id.bt_exportar);
exportar.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_importar:
Intent i = new Intent(this, SincronizarActivity.class);
i.putExtra("cpf", cpf);
i.putExtra("senha", senha);
startActivity(i);
break;
case R.id.bt_exportar:
new Thread(new Runnable() {
public void run() {
Gson gson = new Gson();
final String json = gson.toJson(pdao.exportaPontosUsuario(cpf));
ExportarDados exp = new ExportarDados("{\"teste\":\"java\"}");
String b = exp.ExportaDadosUser();
}
}).start();
break;
}
}
}
And here is the class to Export
public class ExportarDados {
private static final String SOAP_ACTION = "http://serv.lageo.ufpr.br/EnviaPontosUsuario";
private static final String METHOD_NAME = "EnviaPontosUsuario";
private static final String NAMESPACE = "http://serv.lageo.ufpr.br/";
private static final String URL = "http://200.17.203.150/Caderneta/Sincronizar.asmx";
private String json;
private SoapObject soapObject;
private String result = "";
public ExportarDados(String json) {
this.json = json;
}
public String ExportaDadosUser() {
String e2;
try {
soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
soapObject.addProperty("json", json);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(soapObject);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
result = resultString.toString();
} catch(Exception e) {
e.printStackTrace();
}
return result;
}
}
It was the NAMESPACE. It was not suposed to have an / in the end. so the Namespace is http://serv.lageo.ufpr.br instead of http://serv.lageo.ufpr.br/...
I connected Android java with WCF Service. Now I am trying to get data from WCF Service into my project.
I have problem with DataTable type from C# which I have to parse into my Class called Groups
Now I have error with serialization:
java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
cannot be cast to org.ksoap2.serialization.SoapPrimitive
WebService (WCF)
var sp = new StoreProcEgzequtor("[dbo].GetAddonsTypes");
string a = sp.SqlCommand.Connection.Database;
DataTable dt = sp.ExecuteDataTable("Tabela");
return dt;
Class Groups
public class Groups {
private long id;
private long ID2;
private int flgW;
private int flgO;
private String Name;
Activity
public class AndroidWSClientActivity extends Activity {
private static final String METHOD_NAME = "GetAddonsTypes";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:53432/Service1.svc?wsdl";
final String SOAP_ACTION = "http://tempuri.org/IService1/GetAddonsTypes";
TextView textView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wsclient_page);
textView = (TextView) findViewById(R.id.textView2);
Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
final String str = response.toString();
runOnUiThread (new Runnable(){
public void run() {
Log.e("OK",str.toString());
}
});
}
catch (Exception e) {
Log.e("WS", e.toString());
}
}
};
networkThread.start();
}
}
Your error is with the below line. Its clear in the exception. Please check it
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
envelope.getResponse() returns the SoapObject and your are casting it to SoapPrimitive.
I have an Android activity to display the output of a web service. I've called the web service inside another private class which extends AsyncTask, and want to return a value to the main UI thread.
This is my activity.
public class CallCalcService extends Activity {
private String METHOD_NAME = "sum"; // our webservice method name
private String NAMESPACE = "http://backend.android.web.org";
private String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private static final String URL = "http://192.168.1.14:8080/AndroidBackend1/services/Calculate?wsdl";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_calc_service);
//TextView tv = (TextView) findViewById(R.id.textView1);
//Object res=new HardWorkThread().execute();
//tv.setText("Addition : "+ res.toString());
new HardWorkThread()
{
public void onPostExecute(String result)
{
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText("Addition : "+result);
}
}.execute("");
}
private class HardWorkThread extends AsyncTask{
#Override
protected String doInBackground(Object... params) {
// TODO Auto-generated method stub
String result=null;
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("i", 5);
request.addProperty("j", 15);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION,envelope);
result = envelope.getResponse().toString();
System.out.println("Result : " + result);
//((TextView) findViewById(R.id.textView1)).setText("Addition : "+ result.toString());
} catch (Exception E) {
E.printStackTrace();
//((TextView) findViewById(R.id.textView1)).setText("ERROR:"
// + E.getClass().getName() + ":" + E.getMessage());
}
return result;
}
}
}
What I want to do is return the String result value to the main UI, so that I can set that value to the textview.
String result;
TextView tv=findViewById(R.id.textView1);
tv.setText(result);
How might I do this?
You should Override the onPostExecute() method.
Something like this :
#Override
protected void onPostExecute(String result) {
TextView tv = findViewById(R.id.textView1);
tv.setText(result);
}
I am sending details to be stored in a database using a web service using KSOAP. I have used visual studio to create the web service. The web service works fine. A string will be returned when the details have been inserted into the database. The problem is that this string is empty, maybe something is wrong in the way that i am getting the response. I have been trying to find out whats wrong for a long time. please help
public class Registration extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/register";
private static final String OPERATION_NAME = "register";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:58076/WebSite1/Service.asmx";
Button sqlRegister, sqlView;
EditText sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
sqlFirstName = (EditText) findViewById(R.id.etFname);
sqlLastName = (EditText) findViewById(R.id.etLname);
sqlEmail = (EditText) findViewById(R.id.etEmail);
sqlMobileNumber = (EditText) findViewById(R.id.etPhone);
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc);
sqlUsername = (EditText) findViewById(R.id.etUsername);
sqlPassword = (EditText) findViewById(R.id.etPwd);
sqlRegister = (Button) findViewById(R.id.bRegister);
sqlRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()){
case R.id.bRegister:
new LongOperation().execute("");
break;
}
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String firstname = sqlFirstName.getText().toString();
String lastname = sqlLastName.getText().toString();
String emailadd = sqlEmail.getText().toString();
String number = sqlMobileNumber.getText().toString();
String loc = sqlCurrentLocation.getText().toString();
String uname = sqlUsername.getText().toString();
String pwd = sqlPassword.getText().toString();
SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
Request.addProperty("fname", String.valueOf(firstname));
Request.addProperty("lname", String.valueOf(lastname));
Request.addProperty("email", String.valueOf(emailadd));
Request.addProperty("num", String.valueOf(number));
Request.addProperty("loc", String.valueOf(loc));
Request.addProperty("username", String.valueOf(uname));
Request.addProperty("password", String.valueOf(pwd));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Log.d("work","work");
try
{
httpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
String result = response.getProperty(0).toString();
Log.d("res",result);
if(result.equals("reg"))
{
Log.d("reg","reg");
return "Registered";
}
else
{
Log.d("no","no");
return "Not Registered";
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
Log.d("tag","onpost");
if(result!=null)
{
if(result.equals("Registered"))
{
Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
}
else if(result.equals("Not Registered"))
{
Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show(); ///This is what gets printed on screen
}
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
}
Your webservice is returning a String.
Try using this to solve your problem
Object result = envelope.getResponse();
when your webservice return values of type byte[] ,you can do this:
SoapObject response=(SoapObject)envelope.bodyIn;
Hope it helps
I am trying to save the username in shared preferences. I used a web service to get the username and password from the database and it works perfectly. The problem is with saving the username in shared preferences. I have given the code below. I have commented the place where the problem arises. I get the following exception:
java.lang.NullPointerException
I cant find whats wrong. please help.
///Login Class////
public class Login extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/checkLogin";
private static final String OPERATION_NAME = "checkLogin";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";
Button sqllogin;
EditText sqlusername, sqlpassword;
TextView tvData1;
CheckBox cb;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sqlusername = (EditText) findViewById(R.id.etuname1);
sqlpassword = (EditText) findViewById(R.id.etpass);
tvData1 = (TextView)findViewById(R.id.textView1);
sqllogin = (Button) findViewById(R.id.bLogin);
sqllogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()){
case R.id.bLogin:
String username = sqlusername.getText().toString();
String password = sqlpassword.getText().toString();
SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
Request.addProperty("uname", String.valueOf(username));
Request.addProperty("pwd", String.valueOf(password));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try {
httpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
int response = Integer.parseInt(result.getProperty(0).toString());
if(response == 2)//Response is 2 when username and password valid
{
tvData1.setText("You have logged in successfully");
savePrefs("CHECKBOX",cb.isChecked()); //This line and the following 3 lines is where the problem is.
if (cb.isChecked())
{
savePrefs("NAME",sqlusername.getText().toString());
}
Intent openhomepage = new Intent("com.android.disasterAlertApp.HOME");
startActivity(openhomepage);
}
else
{
tvData1.setText("Invalid Username or password");
}
} catch (Exception exception) {
tvData1.setText(exception.toString());
}
break;
}
}
});
}
private void savePrefs(String key, boolean value){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePrefs(String key,String value){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
}
I dont see where you made cb (the check box ) attached to a view?
You are getting a null pointer because cb is nothing. You haven't found its view yet its just a member and that's it. And since this is also where you are getting your null pointer this is almost certainly what's going wrong.
CheckBox cb is not set! use cb = (CheckBox) findViewById(R.id.somewhat);