Well, I'm trying to make an application that logs into a database, I was previously using the thread method to get it working, but at the time of reporting the error messages on the screen or something, it was not. I have modified it to use asyncTaks, but it is continuing to error.
LOG USING ASSYNC:
05-25 08:32:14.444 4091-4091/com.example.hotsystems.hs_celulas E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.hotsystems.hs_celulas, PID: 4091
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:843)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.hotsystems.hs_celulas.Connection.getSetDataWeb(Connection.java:31)
at com.example.hotsystems.hs_celulas.MainActivity.callServer(MainActivity.java:121)
at com.example.hotsystems.hs_celulas.MainActivity.access$000(MainActivity.java:17)
at com.example.hotsystems.hs_celulas.MainActivity$1.onClick(MainActivity.java:51)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
My main code looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hs_cell);
//Chamar os objetos
email = (EditText) findViewById(R.id.TXT_EMAIL_LOGIN);
senha = (EditText) findViewById(R.id.TXT_SENHA_LOGIN);
cod_igrej = (EditText) findViewById(R.id.COD_IGREJ_LOGIN);
entrar = (Button) findViewById(R.id.BTN_ENTRA_APLIC);
sair = (Button) findViewById(R.id.BTN_SAIRX_APLIC);
entrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Usuario usr = new Usuario();
String json = generateJson(usr);
callServer("send-json", json);
}
});
And the part of callServer:
private void callServer(final String method, final String data){
final ProgressDialog progresso = new ProgressDialog(this);
progresso.setMessage("Entrando");
progresso.show();
asw = Connection.getSetDataWeb("http://192.168.1.20/renan/process.php", method, data);
new AsyncTask<String, String, String>(){
protected void onPreExecute(){
progresso.setMessage("Aguarde");
}
protected String doInBackground(String... asw){
if(asw.equals("3"))
progresso.setMessage("Creaódigo da igreja inesistente, por favor escreva um código válido.");
else if (asw.equals("2")){
progresso.setMessage("Senha incorreto, por favor escreva uma senha válida.");
// makeText(MainActivity.this, "Senha incorreto, por favor escreva uma senha válida.", LENGTH_SHORT).show();
}else if (asw.equals("1"))
{
progresso.setMessage("E-mail incorreto, por favor escreva um email válido.");
// makeText(MainActivity.this, "E-mail incorreto, por favor escreva um email válido.", LENGTH_SHORT).show();
}
return null;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(){
progresso.setMessage("Seja Bem vindo.");
progresso.show();
Intent it = new Intent(MainActivity.this, MC_Home.class);
startActivity(it);
progresso.dismiss();
}
/*public void run(){
if (data.isEmpty()){
degenerateJson(asw);
}
Atividade_Entrar(asw, null,null);
//makeText(MainActivity.this, "Código da igreja inesistente, por favor escreva um código válido.", LENGTH_SHORT).show();
}*/
}.execute();
}
}
Class Conection
:
package com.example.hotsystems.hs_celulas;
import android.app.Activity;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class Connection extends Activity{
public static String getSetDataWeb(String url, String method, String data){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String answer = "";
try{
ArrayList<NameValuePair> valores = new ArrayList<NameValuePair>();
valores.add(new BasicNameValuePair("method", method));
valores.add(new BasicNameValuePair("json", data));
httpPost.setEntity(new UrlEncodedFormEntity(valores));
HttpResponse resposta = httpClient.execute(httpPost);
answer = EntityUtils.toString(resposta.getEntity());
}
catch(NullPointerException e){ e.printStackTrace(); }
catch(ClientProtocolException e){ e.printStackTrace(); }
catch(IOException e){ e.printStackTrace(); }
return(answer);
}
}