Kandarp Pandya

Kandarp Pandya

  • NA
  • 12
  • 1.5k

for weight convter

Feb 2 2016 1:54 AM
package com.example.json;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

import com.example.websirvice.R;

public class DotNetWebService extends Activity {
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
private final String METHOD_NAME = "ConvertWeight";
private static String gram;
private static String kilogram;
private String TAG = "PGGURU";
Button b;
TextView tv;
EditText et;

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

//gram Edit Control
et = (EditText) findViewById(R.id.editText1);
//hrenheit Text control
tv = (TextView) findViewById(R.id.webserviceResponse);
//Button to trigger web service invocation
b = (Button) findViewById(R.id.webservice);
//Button Click Listener
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.getText().length() != 0 && et.getText().toString() != "") {
//Get the text control value
gram = et.getText().toString();
System.out.println(gram);
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
} else {
tv.setText("Please enter gram");
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.menu_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

private class AsyncCallWS extends AsyncTask<String ,Void ,Void>{
@Override
protected Void doInBackground(String... params) {
Log.i(TAG,"doInBackground");
getkilogram(gram);
return null;
}
protected void onPostExecute(Void result){
Log.i(TAG,"onPostExecute");
tv.setText(kilogram+ "kg");
}
protected void onPreExecute(){
Log.i(TAG,"onPreExecutr");
tv.setText("Calculating...");
}
protected void onProgressUpdate(Void... values){
Log.i(TAG,"onProgressUpdate");
}
}

public void getkilogram(String gram) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo gramPI = new PropertyInfo();
//Set Name
gramPI.setName("grams");
//Set Value
gramPI.setValue(gram);
//Set dataType
gramPI.setType(double.class);
//Add the property to request object
request.addProperty(gramPI);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
kilogram= response.toString();

} catch (Exception e) {
e.printStackTrace();
}
}
}
 
i enter any value in edit text but i only see in output "0"

Answers (1)