如何修复空对象引用上的 java.lang.String org.json.JSONObject.toString()'?
How to Fix java.lang.String org.json.JSONObject.toString()' on a null object reference?
我的 android 模拟器在尝试 运行 登录 activity 时,每当我尝试 运行 时都会返回空对象引用。
一旦到达以下行,它就会这样做:
// check your log for json response
Log.d("Login attempt", json.toString());
这是完整的 login.java 代码:
public class login extends Activity implements View.OnClickListener {
private AnimatedGifImageView animatedGifImageView;
public EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://www.samplephppage.com";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/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_login);
user = (EditText)findViewById(R.id.etUsername);
pass = (EditText)findViewById(R.id.etPassword);
animatedGifImageView = ((AnimatedGifImageView) findViewById(R.id.animatedGifImageView));
animatedGifImageView.setAnimatedGif(R.raw.animated_gif_big, TYPE.AS_IS);
//setup input fields
//setup buttons
mSubmit = (Button) findViewById(R.id.btnLogin);
//register listeners
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnLogin:
new AttemptLogin().execute();
break;
case R.id.textView6:
Intent i = new Intent(this, forgotpassword.class);
startActivity(i);
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(login.this);
pDialog.setMessage("Checking Credentials...");
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 etUsername = user.getText().toString();
String etPassword = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", etUsername));
params.add(new BasicNameValuePair("password", etPassword));
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());** <--- **Crashes Here**
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(login.this, LoginLoading.class);
finish();
startActivity(i);
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;
}
这是来自 php 页面的代码
<?php
if($count == 1){
$response['success'] = 1;
$response['message'] = "Login Successful"
die(json_encode($response));
}
?>
我似乎可以弄清楚为什么它给我这个异常?
因为这个:
private static final String LOGIN_URL = "my php page";
不是有效的 URL。
阅读该行上方的评论,以获取关于您的 LOGIN_URL 应分配给什么的建议。
确保您的登录 URL + 您的参数合乎逻辑。在您的代码中注销并在您的浏览器中执行 post 请求以查看该请求是否实际上返回了您想要的 JSON。
只需在任何浏览器中点击 LOGIN_URL
即可检查您的 Web 服务实现是否为您提供了 JSON 中的响应。
如果响应出现,那么也可以在您的应用程序中尝试,否则请检查您的网络服务中是否有错误
我的 android 模拟器在尝试 运行 登录 activity 时,每当我尝试 运行 时都会返回空对象引用。
一旦到达以下行,它就会这样做:
// check your log for json response
Log.d("Login attempt", json.toString());
这是完整的 login.java 代码:
public class login extends Activity implements View.OnClickListener {
private AnimatedGifImageView animatedGifImageView;
public EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://www.samplephppage.com";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/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_login);
user = (EditText)findViewById(R.id.etUsername);
pass = (EditText)findViewById(R.id.etPassword);
animatedGifImageView = ((AnimatedGifImageView) findViewById(R.id.animatedGifImageView));
animatedGifImageView.setAnimatedGif(R.raw.animated_gif_big, TYPE.AS_IS);
//setup input fields
//setup buttons
mSubmit = (Button) findViewById(R.id.btnLogin);
//register listeners
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnLogin:
new AttemptLogin().execute();
break;
case R.id.textView6:
Intent i = new Intent(this, forgotpassword.class);
startActivity(i);
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(login.this);
pDialog.setMessage("Checking Credentials...");
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 etUsername = user.getText().toString();
String etPassword = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", etUsername));
params.add(new BasicNameValuePair("password", etPassword));
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());** <--- **Crashes Here**
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(login.this, LoginLoading.class);
finish();
startActivity(i);
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;
}
这是来自 php 页面的代码
<?php
if($count == 1){
$response['success'] = 1;
$response['message'] = "Login Successful"
die(json_encode($response));
}
?>
我似乎可以弄清楚为什么它给我这个异常?
因为这个:
private static final String LOGIN_URL = "my php page";
不是有效的 URL。
阅读该行上方的评论,以获取关于您的 LOGIN_URL 应分配给什么的建议。
确保您的登录 URL + 您的参数合乎逻辑。在您的代码中注销并在您的浏览器中执行 post 请求以查看该请求是否实际上返回了您想要的 JSON。
只需在任何浏览器中点击 LOGIN_URL
即可检查您的 Web 服务实现是否为您提供了 JSON 中的响应。
如果响应出现,那么也可以在您的应用程序中尝试,否则请检查您的网络服务中是否有错误