JSON Android java.lang.NullPointerException
我在应用程序运行活动时崩溃JSON Android java.lang.NullPointerException,java,php,android,json,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
这里有一些关于我的应用程序代码的详细信息
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();
}
}
}
}
数据库功能
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;
}
}
我的信息表登录我是android编程的新手,还在学习..
请帮助我我的代码出了什么问题 编辑:正在工作 只需放置die(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!";
?>
谢谢你帮我提出建议你应该像这样返回json:
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
否则,php代码中的Return语句将不起作用!尝试实现echo或print。请粘贴json响应字符串。我不知道如何获取json响应字符串。。你能解释一下这个步骤吗?兄弟你的url响应。。这是我的错,在使用实现json代码时还是新手。我已经编辑了你的代码,现在告诉我什么是responsemy代码可以返回的值,但我只是忘了在json响应的末尾加上die(json_encode($response));
<?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!";
?>
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));