2013-02-06 33 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; 
    } 

} 

logcat的 http://pastebin.com/0xVv2pBR登錄與web服務的Android使用JSON

當我點擊按鈕登錄應用程序崩潰,我真的不明白什麼是錯的,如果你需要更多的信息,只是問我。如果你知道如何解決請幫助我。 感謝

回答

0

日誌說

「02-06 18:50:47.785:I /編舞(874):跳過41幀的應用程序可以做它的主線程的工作太多了」

如果您是Android新手,則必須學會在Activity中執行AsyncTask。您不可以在main/UIthread中使用web服務或http通信。在這裏我發現Tutorial to implement AsyncTask請了解它會解決你的問題。

+0

要試試看,謝謝! –