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
当我点击按钮登录应用程序崩溃时,我真的无法理解有什么问题,如果您需要更多信息,请问我。如果你知道如何解决帮助我请。 感谢
答案 0 :(得分:0)
日志说
“02-06 18:50:47.785:I / Choreographer(874):跳过41帧!应用程序可能在其主线程上做了太多工作”
如果您不熟悉Android,则必须学会在Activity
中实施AsyncTask。您不能在main/UI
thread
中使用网络服务或http通信。在这里,我发现Tutorial to implement AsyncTask请了解它将解决您的问题。