Hello ,
i Think I cant pass correct parameter for weight converter Please Help me
I use http://www.webservicex.net/ConvertWeight.asmx Website
And My Code Is
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 android.widget.Toast;
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 Grams;
private static String Kilograms;
private String TAG = "PGGURU";
Button b;
TextView tv;
EditText Weight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convertweight);
//gram Edit Control
Weight = (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 (Weight.getText().length() != 0 && Weight.getText().toString() != "") {
//Get the text control value
Grams = Weight.getText().toString();
System.out.println(Grams);
// Toast.makeText(getApplicationContext(),"gram enter" , Toast.LENGTH_LONG).show();
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
// Toast.makeText(getApplicationContext(),"task execute" , Toast.LENGTH_LONG).show();
} 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_websirvice_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");
getKilograms(Grams);
//Toast.makeText(getApplicationContext(),"background" , Toast.LENGTH_LONG).show();
return null;
}
protected void onPostExecute(Void result){
Log.i(TAG,"onPostExecute");
tv.setText(Kilograms+ "kg");
// Toast.makeText(getApplicationContext(),"postexecute" , Toast.LENGTH_LONG).show();
}
protected void onPreExecute(){
// Log.i(TAG,"onPreExecutr");
tv.setText("Calculating...");
// Toast.makeText(getApplicationContext(),"preexecute" , Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Void... values){
// Log.i(TAG,"onProgressUpdate");
//Toast.makeText(getApplicationContext(),"OnprogressUpdate" , Toast.LENGTH_LONG).show();
}
}
public void getKilograms(String Grams) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo GramsPI = new PropertyInfo();
//Set Name
GramsPI.setName("Grams");
//Set Value
GramsPI.setValue(Grams);
//Set dataType
GramsPI.setType(double.class);
//Add the property to request object
request.addProperty(GramsPI);
//Toast.makeText(getApplicationContext(),"Gramspi" , Toast.LENGTH_LONG).show();
//Create envelope
SoapSerializationEnvelope Envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
Envelope.dotNet = true;
//Set output SOAP object
// Toast.makeText(getApplicationContext(),"Envelope" , Toast.LENGTH_LONG).show();
Envelope.setOutputSoapObject(request);
//Toast.makeText(getApplicationContext(),"Enveloperequest" , Toast.LENGTH_LONG).show();
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, Envelope);
//Get the response
// Toast.makeText(getApplicationContext(),"transportcall" , Toast.LENGTH_LONG).show();
SoapPrimitive response = (SoapPrimitive) Envelope.getResponse();
//Assign it to fahren static variable
Kilograms= response.toString();
Toast.makeText(getApplicationContext(),"response" , Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
// Toast.makeText(getApplicationContext(),"exception" , Toast.LENGTH_LONG).show();
}
}
}