2013-02-06 3 views
0
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; 
    } 

} 

로그 캣 http://pastebin.com/0xVv2pBR로그인하여 JSON

내가 응용 프로그램 충돌을 로그인 버튼을 클릭하면 그냥 물어 자세한 정보가 필요하면 내가 정말 잘못이 무엇인지 이해할 수없는 경우 나를. 해결 방법을 알고 있다면 제발 도와주세요. 감사

답변

0

로그는 말한다

"02-06 18 : 50 : 47.785 : I/안무가 (874) :! 건너 뛴 41 프레임 응용 프로그램의 주 스레드에서 너무 많은 일을 할 수있다"

Android를 처음 사용하는 경우 ActivityAsyncTask을 구현하는 방법을 배워야합니다. main/UIthread에 웹 서비스 또는 http 통신을 사용하지 않는 것이 좋습니다. 여기 내가 찾은 Tutorial to implement AsyncTask 당신의 문제를 해결할 것입니다 배우십시오.

+0

시도해 보겠습니다. 고마워요! –

관련 문제