I’ve an android application.
I need to execute a code on a separate task because it fives me android.os.NetworkOnMainThreadException .
This is my code:
public class AccesoCuentaActivity extends Activity{
public void validacion(View v) throws IOException, XmlPullParserException
{
try {
String usuario=((EditText)findViewById(R.id.etUsu)).getText().toString();
String pass=((EditText)findViewById(R.id.etPass)).getText().toString();
String[] pars = new String[2];
String[] = new String[2];
pars[0] = usuario;
pars[1] = pass;
new ValidacionThread().execute(pars);
Intent intent=new Intent(this, MainActivity.class);
intent.putExtra("idBoton",12);
intent.putExtra("usuario",usu);
startActivityForResult(intent, 0);
} }
Task:
public class ValidacionThread extends AsyncTask<String, Void, Usuario> {
private final String NAMESPACE = "http://webservices.pcp/";
private final String URL = "http://127.0.0.1:8080/webServices/pcpWS?wsdl";
private final String SOAPACTION = "";
private final String METHOD = "ValidarUsuario";
protected Usuario doInBackground(String[] pars) {
String user = pars[0];
String password = pars[1];
SoapObject request = new SoapObject(NAMESPACE, METHOD);
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sobre.dotNet = false;
request.addProperty("login", user);
request.addProperty("password", password);
sobre.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try {
transporte.call(SOAPACTION, sobre);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SoapObject resultado = null;
try {
resultado = (SoapObject)sobre.getResponse();
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Usuario usu = new Usuario();
usu.IdUsuario= resultado.getProperty(0).toString();
usu.Nombre = resultado.getProperty(1).toString();
usu.Apellidos = resultado.getProperty(2).toString();
usu.Rol = resultado.getProperty(3).toString();
usu.Centro=resultado.getProperty(4).toString();
return usu;
}
protected void onPostExecute(ValidacionThread vt) {
}
}
It gives me two problems:
1) In class AccesoCuentaActivity :
I want to save the variable “usuario” return in ValidationThread.doInBackground(). But how do I do it?
Indeed if I just write: new ValidacionThread().execute(pars); it tells me that the variable “usu” (intent.putExtra(“usuario”,usu);) has not been initialized
while with code (in main code) Usuario usu = new ValidacionThread().execute(pars); it says type mismatch
2) In doInBackground() line eclipse gives the warning:
Varargs methods should only override or be overridden by other varargs methods unlike ValidacionThread.doInBackground(String[]) and AsyncTask<String,Void,Usuario>.doInBackground(String...)
Since this is Async task you need to create a return way for that data.
I would recomend creating an interface like:
interface{public void parseData(Usuario data);
}
and implementing it in your activity class.
I would then pass and instance of activity to that async task in constructor.
Then in your ValidationThread:
protected void onPostExecute(Usuario vt) {activityInstance.parseData(vt);
}
As for the second part change String[] to String..