JSON Android java.lang.NullPointerException

时间:2014-01-15 04:49:24

标签: java php android json

我正在运行活动的应用程序崩溃。
这里有一些信息详细说明我的应用代码

LDActivity.Java

public class LDActivity extends Activity {

    private ProgressDialog pDialog, cDialog;

    JSONParser jsonParser = new JSONParser();
    GlobalVar gvar;

    private static final String LOGIN_URL = "http://deka.esy.es/track_api/";

    private boolean isDualSIM;
    private String iSIM1;
    private String iSIM2;

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

        TelephonyInfo tf = TelephonyInfo.getInstance(this);
        gvar = ((GlobalVar)getApplicationContext());

        isDualSIM = tf.isDualSIM(); gvar.setisDualSIM(isDualSIM);
        iSIM1 = tf.getImeiSIM1(); gvar.setiSIM1(iSIM1);
        iSIM2 = tf.getImeiSIM2(); gvar.setiSIM2(iSIM2);

        if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
            new SessionCheck().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        else
            new SessionCheck().execute();
    }

    private class SessionCheck extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            cDialog = new ProgressDialog(LDActivity.this);
            cDialog.setMessage("Please wait checking login...");
            cDialog.setIndeterminate(false);
            cDialog.setCancelable(false);
            cDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            int csuccess;
            Boolean cDualSIM = gvar.getisDualSIM();
            String cIMEIPhone1, cIMEIPhone2 = "";

            if(cDualSIM) {
                cIMEIPhone1 = gvar.getiSIM1();
                cIMEIPhone2 = gvar.getiSIM2();
            } else
                cIMEIPhone1 = gvar.getiSIM1();

            try {
                List<NameValuePair> cIMEI = new ArrayList<NameValuePair>();
                cIMEI.add(new BasicNameValuePair("tag", TAG_SESI));
                cIMEI.add(new BasicNameValuePair("isim1", cIMEIPhone1));
                if(cDualSIM)
                    cIMEI.add(new BasicNameValuePair("isim2", cIMEIPhone2));

                JSONObject cjson = jsonParser.makeHttpRequest(LOGIN_URL, "POST", cIMEI);
                //Log.d("Session Check ", json.toString());

                csuccess = cjson.getInt(KEY_SUCCESS);
                if (csuccess == 1) {
                    /*Intent dekatrack = new Intent(getApplicationContext(), DekaTrack.class);
                    dekatrack.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    finish();
                    startActivity(dekatrack);*/
                    return cjson.getString(KEY_SUCCESS_MSG);
                } else
                    return cjson.getString(KEY_ERROR_MSG);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

        protected void onPostExecute(String cjson_msg) {
            cDialog.dismiss();
            if (cjson_msg != null) {
                Toast cts = Toast.makeText(LDActivity.this, cjson_msg, Toast.LENGTH_LONG);
                cts.setGravity(Gravity.TOP, 0, 50);
                cts.show();
            }

        }
    }
}

json index.php

<?php header('content-type: application/json; charset=utf-8');

if (isset($_POST['tag']) && $_POST['tag'] != '') {
    $tag = $_POST['tag'];

    require_once 'lib/db_inc.php';
    $db = new DB_Functions();

    $response = array("tag" => $tag, "sukses" => 0, "error" => 0);

    $isim1 = $_POST['isim1'];
    $isim2 = $_POST['isim2'];

    if ($tag == 'sesi') {
        $session1 = $db->getSession($isim1);
        if(empty($isim2))
            $session2 = "0";
        else
            $session2 = $db->getSession($isim2);

        if($session1 == "1" || $session2 == "1") {
            $response["sukses"] = 1;
            $response["sukses_msg"] = "Auto login!";
        } else if ($session1 == "0" || $session2 == "0") {
            $response["error"] = 1;
            $response["error_msg"] = "Silahkan login!";
        } else {
            $response["error"] = 2;
            $response["error_msg"] = "Silahkan daftar!";
        }
    } else 
        echo "Permintaan akses gagal!";
} else
    echo "Permintaan akses ditolak!";
?>

错误LogCat

01-15 10:11:39.343: E/JSON Parser(1331): Error parsing data org.json.JSONException: End of input at character 0 of 
01-15 10:11:39.453: E/AndroidRuntime(1331): FATAL EXCEPTION: AsyncTask #1
01-15 10:11:39.453: E/AndroidRuntime(1331): java.lang.RuntimeException: An error occured while executing doInBackground()
01-15 10:11:39.453: E/AndroidRuntime(1331):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.lang.Thread.run(Thread.java:856)
01-15 10:11:39.453: E/AndroidRuntime(1331): Caused by: java.lang.NullPointerException
01-15 10:11:39.453: E/AndroidRuntime(1331):     at com.skripsi.dekatrack.LDActivity$SessionCheck.doInBackground(LDActivity.java:143)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at com.skripsi.dekatrack.LDActivity$SessionCheck.doInBackground(LDActivity.java:1)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-15 10:11:39.453: E/AndroidRuntime(1331):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-15 10:11:39.453: E/AndroidRuntime(1331):     ... 4 more

数据库功能

public function getSession($imei) {
    $result = mysql_query("SELECT session FROM login WHERE imei = '$imei'") or die(mysql_error());
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysql_fetch_array($result);
        return $result['session'];
    } else {
        return false;
    }
}

我的信息表登录
table login

我是编程android的新手,还在学习..
请帮我解决我的代码有什么问题..

编辑:正在运作

刚放死(json_encode($ response));在响应结束时json代码和一点点调整代码..

<?php header('content-type: application/json; charset=utf-8');

if (isset($_POST['tag']) && $_POST['tag'] != '') {
    $tag = $_POST['tag'];

    require_once 'lib/db_inc.php';
    $db = new DB_Functions();

    $response = array("tag" => $tag, "sukses" => 0, "error" => 0);

    $isim1 = $_POST['isim1'];
    $isim2 = $_POST['isim2'];

    $session1 = $db->getSession($isim1);
    $session2 = $db->getSession($isim2);


    if ($tag == 'sesi') {
        if(!empty($session1) || !empty($session2)) {
            if($session1 == "1" || $session2 == "1") {
                $response["sukses"] = 1;
                $response["sukses_msg"] = "Auto login!";
                die(json_encode($response));
            } else if ($session1 == "0" || $session2 == "0") {
                $response["error"] = 1;
                $response["error_msg"] = "Silahkan login!";
                die(json_encode($response));
            } else {
                $response["error"] = 2;
                $response["error_msg"] = "Silahkan daftar!";
                die(json_encode($response));
            }
        } else {
            $response["error"] = 1;
            $response["error_msg"] = "HP anda rusak!";
            die(json_encode($response));
        }
    } else 
        echo "Permintaan akses gagal!";
} else
    echo "Permintaan akses ditolak!";
?>



所有这些都是为了帮助我提出建议

1 个答案:

答案 0 :(得分:1)

你应该像这样返回json:

while($row=mysql_fetch_assoc($result))
    $output[]=$row;
print(json_encode($output));

否则,php代码中的return语句将无效!尝试实现echo或print。