0
I have a Web Service created in C# where I access a certain method to authenticate a teacher, if the user and password data passed through parameters exiting in the database the web service returns me the following data [Codprof],[name],[User], [Password].
I am able to pass these parameters through a login activity perfectly and all data is being returned.
The problem is that the entire web service access and return code for this data is in the Activity class.
I wonder if there’s a way I can structure my classes to be more organized.
Example:
Teacher Class: Where all teacher’s get and set attributes and methods will be contained.
Professorws class: Where you will receive the parameters of Activity and communicate with the web service returning an object of the teacher type.
Activity class responsible for capturing typed data and passing to Professorws.
Code:
package feol.com.br.diarioescolar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
public class Login extends Activity {
private Button btEntrar;
private EditText edUsuario, edSenha;
private ProgressDialog dialogo;
String codigo;
String nome;
String usuario ;
String senha ;
private static String SOAP_ACTION ="http://feol/AutenticarProfessor";
private static String NAMESPACE = "http://feol/";
private static String METHOD_NAME= "AutenticarProfessor";
private static String URL = "http://192.168.43.175/ServiceFeol.asmx?WSDL";
Professor p = new Professor();
ProfessorWs ws = new ProfessorWs();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btEntrar = (Button)findViewById(R.id.btEntrar);
edUsuario = (EditText)findViewById(R.id.edUsuario);
edSenha = (EditText)findViewById(R.id.edSenha);
btEntrar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btEntrarOnClick();
}
});
}
private void btEntrarOnClick(){
new asynProf().execute();
}
public Boolean invocaWs(){
Boolean re = true;
try{
SoapObject resposta = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
resposta.addProperty("user",edUsuario.getText().toString());
resposta.addProperty("senha",edSenha.getText().toString());
envelope.setOutputSoapObject(resposta);
HttpTransportSE http = new HttpTransportSE(URL);
http.call(SOAP_ACTION,envelope);
String resultado = envelope.getResponse().toString();
JSONArray jsonArray = new JSONArray(resultado);
for(int i=0;i<jsonArray.length();i++ ) {
JSONObject jsonObject =jsonArray.getJSONObject(i);
codigo = jsonObject.getString("CodProf");
nome = jsonObject.getString("Nome");
usuario = jsonObject.getString("Usuario");
senha = jsonObject.getJSONObject("Senha").toString();
}
} catch (IOException e){
e.printStackTrace();
re=false;
}catch (XmlPullParserException e){
e.printStackTrace();
re=false;
}catch (JSONException e){
e.printStackTrace();
}
return re;
}
class asynProf extends AsyncTask<String,String,String> {
@Override
protected void onPreExecute(){
dialogo = new ProgressDialog(Login.this);
dialogo.setMessage("Carregando codigo...");
dialogo.setIndeterminate(false);
dialogo.setCancelable(false);
dialogo.show();
}
@Override
protected String doInBackground (String... strings){
if (invocaWs())
{return "ok";
}else {return "erro";}
}
@Override
protected void onPostExecute(String s){
dialogo.dismiss();
if(s.equals("ok")){
mostraCod();
}else {
Log.e("Script","DeuErrado" + s.toString());}
}
}
public void mostraCod(){
Toast t = Toast.makeText(getBaseContext(),"Código pro: " +nome, Toast.LENGTH_SHORT);
t.show();
}
}