2013-08-06 48 views
0

您好,我用正確的憑據按登錄按鈕後出現錯誤。 dbconnection被正確設置並且php API正在工作。我在網頁瀏覽器上試過。安卓應用程序在登錄時遇到錯誤

下面是Java代碼:

package com.example.test; 


import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 

import com.capstone.istudyUser.SProfile; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener { 

    private EditText user, pass; 
    private Button mSubmit, mRegister; 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 

    private static final String LOGIN_URL = "http://192.168.42.59/test/login.php"; 


    //JSON element ids from repsonse of php script: 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "message"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     user = (EditText)findViewById(R.id.iUsername); 
     pass = (EditText)findViewById(R.id.iPassword); 

     mSubmit = (Button)findViewById(R.id.bLogin); 
     mRegister = (Button)findViewById(R.id.RStudent); 

     mSubmit.setOnClickListener(this); 
     mRegister.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch (v.getId()) { 
     case R.id.bLogin: 
      new AttemptLogin().execute(); 
      break; 
     case R.id.RStudent: 

      break; 

     default: 
      break; 
     } 
    } 

    class AttemptLogin extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     boolean failure = false; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Attempting login..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... args) { 
      // TODO Auto-generated method stub 
      // Check for success tag 
      int success; 
      String username = user.getText().toString(); 
      String password = pass.getText().toString(); 
      try { 
       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("username", username)); 
       params.add(new BasicNameValuePair("password", password)); 

       Log.d("request!", "starting"); 
       // getting product details by making HTTP request 
       JSONObject json = jsonParser.makeHttpRequest(
         LOGIN_URL, "POST", params); 

       // check your log for json response 
       Log.d("Login attempt", json.toString()); 

       // json success tag 
       success = json.getInt(TAG_SUCCESS); 
       if (success == 1) { 
       Log.d("Login Successful!", json.toString()); 
       return json.getString(TAG_MESSAGE); 
       }else{ 
       Log.d("Login Failure!", json.getString(TAG_MESSAGE)); 
       return json.getString(TAG_MESSAGE); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 

     } 
     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once product deleted 
      pDialog.dismiss(); 
      if (file_url != null){ 
      Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show(); 
      } 

     } 

    } 



} 

我不知道這有什麼錯我的程序:(

這裏是日誌貓: http://pastebin.com/wzXzCShH

+0

發佈您的logcat輸出以查看您得到的異常。 –

+0

您是否在LogCat中遇到任何異常?你嘗試調試它嗎? 如果您嘗試使用模擬器,則可能無法訪問與您的計算機相同的網絡。仿真器有自己的網絡。 – Darwind

+0

我已經嘗試調試它,但我真的不知道。該計劃有什麼問題。我沒有使用模擬器。我在我的電腦上使用與我的手機相同的局域網。 – ChaosDarky

回答

0

確保你給應用程序的INTERNET您的Android清單中的權限,您被拒絕訪問該URL,然後導致NullPointer並導致您的應用程序崩潰的權限

0

我同意NullPointer執行可能是由於未請求Internet權限而導致的。

下面的行添加到您的清單:

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

您應該能夠通過添加額外的catch塊這樣避免了強制關閉,即使它失敗:

} catch (JSONException e) { 
      e.printStackTrace(); 
} catch (NullPointerException npe){ 
    npe.printStackTrace(); 
} 

這將讓您的應用程序繼續優雅。 (這是一件好事,因爲它會覆蓋你,例如當你的php腳本出現時,因此返回沒有有效的JSON字符串,或者由於某種原因,例如設備處於飛行模式或網絡關閉,無法訪問url)

0

@Dandelo Darky Cardona - 確保您的IP地址正在訪問本地主機,嘗試在您的瀏覽器中輸入您的IP地址,並檢查它是否不允許您拒絕或拒絕您的權限 因爲我之前有過這個錯誤,直到我知道在IP地址的問題,我瘋了。

相關問題