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

4

3 回答 3

0

@Dandelo Darky Cardona - 确保您的 IP 地址正在访问本地主机,尝试在您的浏览器中输入您的 IP 地址并检查它是否没有授予您权限被拒绝,因为我之前遇到过这个错误,这让我发疯,直到我知道IP地址的问题。

于 2014-12-22T13:55:36.567 回答
0

确保您已在 Android 清单中为应用授予 INTERNET 权限。您被拒绝访问该 URL 的权限,这会导致 NullPointer 并导致您的应用程序崩溃。

于 2013-08-06T10:09:35.033 回答
0

我同意 NullPointer execption 可能是由于未请求 Internet 许可而引起的。

将以下行添加到您的清单中:

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

即使强制关闭失败,您也应该能够通过添加额外的 catch 块来避免强制关闭,如下所示:

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

这将允许您的应用程序优雅地继续。(这是一件好事,因为当您的 php 脚本被 b0rked 时它会覆盖您,因此不会返回有效的 JSON 字符串,或者由于某种原因(例如设备处于飞行模式或网络中断)而无法访问 url)

于 2013-08-06T10:16:32.797 回答