I have updated my code!
My web Service Class
public class WebService {
private ArrayList<SimpleObserver> listeners;
private static String res;
public WebService() {
listeners = new ArrayList<SimpleObserver>();
}
public void addListener(SimpleObserver obs) {
listeners.add(obs);
}
public void removeListener(SimpleObserver obs) {
listeners.remove(obs);
}
public void notifyListener(String s) {
for (SimpleObserver listener : listeners)
listener.onChange(s);
}
public void callService(String[] URL) {
ServiceAcerola task = new ServiceAcerola();
task.execute(URL);
notifyListener(res);
}
public class ServiceAcerola extends AsyncTask<String, String, String> {
private int responseCode;
private String message;
private String returnResponse;
private String URL;
public String getResponse() {
return returnResponse;
}
public String getErrorMessage() {
return message;
}
public int getResponseCode() {
return responseCode;
}
@Override
protected void onPreExecute() {
// notifyListener("A calcular");
}
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
HttpParams my_httpParams = new BasicHttpParams();
final String proxyHost = android.net.Proxy.getDefaultHost();
final int proxyPort = android.net.Proxy.getDefaultPort();
if (proxyPort != -1) {
my_httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY,
new HttpHost(proxyHost, proxyPort));
}
DefaultHttpClient client = new DefaultHttpClient(my_httpParams);
HttpGet httpGet = new HttpGet(url);
Log.d("URL serviço HttpGet", url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
Log.d("RESPOSTA do web service", response);
returnResponse = response;
} catch (Exception e) {
e.printStackTrace();
response = e.getMessage();
Log.e("ERRO de respota", e.getMessage());
}
}
Log.d("RESPOSTA do web service", response);
return response;
}
@Override
protected void onPostExecute(String result) {
Log.d("onPostExecute Serviço", result);
//notifyListener(result);
res = result;
}
}
}
I have add a sub class with the task I saw in android developers guide.
And this is the class that calls my webService
public class UserDao implements SimpleObserver {
private AndroidFSDS fs;
private static final String path =
"/data/data/pt.agile21.acerola/files/active.txt";
private WebService s;
private static final String URL = "http://URL of Service/acerola?id=";
private Boolean isRegistered;
public static String result;
public UserDao() {
fs = new AndroidFSDS("active.txt");
s = new WebService();
s.addListener(this);
isRegistered = false;
result = null;
}
/**
*
* @param pass
* @param email
*/
public Boolean ActiveAccount(String email) {
String send = email;
executeService(send);
return isRegistered;
}
/**
*
* @param user
* @param pass
*/
public Boolean setPassword(String user, String pass) {
String send = "p;" + user + ";" + pass;
executeService(send);
return isRegistered;
}
public String getUser(String email) {
String send = "g;" + email;
Log.d("parametros a enviar",send);
executeService(send);
Log.d("Chegada", "foi ao serviço");
//return result;
return result;
}
public String isRegistered(Application application) throws IOException {
File f = new File(path);
if (f.exists())
return fs.read(application);
else
return null;
}
public void setRegistered(Application application, String email) {
fs.write(email, application);
}
public void onChange(String s) {
//testes
Log.d("onChange", "entrou no onChange "+s);
if(s.equals("Sucesso"))
isRegistered = true;
else if(s.equals("Insucesso"))
isRegistered = false;
result = s;
}
public void executeService(String param) {
try {
Log.d("Entrar", "no serviço");
new WebService().callService(new String [] {URL+param});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Erro ao aceder ao web service", e.getMessage());
}
}
}
This is called for my activity ! Someone can help me.. I'm tring to use de
MVC pattern.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en