package com.example.friendfinder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class login extends Activity implements OnClickListener{
String APIurl = "http://friendfinder.hostzi.com/ws/";
Button btnlogin;
TextView login_user;
TextView login_pass;
TextView tv;
JSONObject jObject;
AlertDialog.Builder builder;
AlertDialog alert;
ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
start();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_entrar:
if (checkStatus()) {
progressDialog.show();
new Thread(new Runnable() {
public void run() {
try {
jObject = new JSONObject(getJSON("login.php?email="
+ login_user.getText().toString()
+ "&password="
+ login_pass.getText().toString()));
tv.setText(jObject.toString());
Log.e("JSON", jObject.toString());
if (jObject.getString("success").equalsIgnoreCase(
"true")) {
progressDialog.dismiss();
confirm();
} else if (jObject.getString("success")
.equalsIgnoreCase("false")) {
login.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
builder.setMessage("O inicio de sessão falhou, verifica os teus dados.");
alert = builder.create();
alert.show();
}
});
login.this.runOnUiThread(null);
} else {
login.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
builder.setMessage("O inicio de sessão falhou, verifica os teus dados.");
alert = builder.create();
alert.show();
}
});
login.this.runOnUiThread(null);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
else{
builder.setMessage("Não existe de momento nenhuma conexão à Internet! O ínicio de sessão é impossivel de realizar");
alert = builder.create();
alert.show();
}
}
}
public void start(){
login_user = (TextView) findViewById(R.id.login_user);
login_pass = (TextView) findViewById(R.id.login_pass);
tv = (TextView) findViewById(R.id.tv);
btnlogin = (Button) findViewById(R.id.btn_entrar);
btnlogin.setOnClickListener(this);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("A iniciar sessão...");
builder = new AlertDialog.Builder(login.this);
builder.setCancelable(false);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
}
public void confirm(){
Intent i = new Intent(login.this, maps.class);
startActivity(i);
finish();
}
public String getJSON(String rurl) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(APIurl + rurl);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
public boolean checkStatus() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
}
LogCat
http://pastebin.com/0xVv2pBR
When i click the button to login the app crashes and i realy can’t understand what is wrong, if you need more information just ask me. If you know how to resolve help me please.
Thanks
logs says
if you are new to Android you must learn to implement AsyncTask in your
Activity. You are not suppose to use webservice or http communication inmain/UIthread. here i found Tutorial to implement AsyncTask please learn it will solve your problem.