Php 如何修复java.lang.String org.json.JSONObject.toString();在空对象引用上?
我的android emulator在尝试运行登录活动时,每当我尝试运行它时,都会抛出null对象引用 一旦到达以下行,它就会这样做:Php 如何修复java.lang.String org.json.JSONObject.toString();在空对象引用上?,php,android,sql,json,login,Php,Android,Sql,Json,Login,我的android emulator在尝试运行登录活动时,每当我尝试运行它时,都会抛出null对象引用 一旦到达以下行,它就会这样做: // check your log for json response Log.d("Login attempt", json.toString()); 下面是完整的login.java代码: public class login extends Activity implements View.OnClickListener {
// 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;
}
公共类登录扩展活动实现View.OnClickListener{
私有animatedgimageview animatedgimageview;
公共编辑文本用户,通过;
私人按钮mSubmit,mRegister;
//进度对话框
私人对话;
JSONParser JSONParser=新的JSONParser();
//php登录脚本位置:
//本地主机:
//在您的设备上进行测试
//将本地ip改为在windows上运行CMD>ipconfig
//或者在mac的终端类型ifconfig中,查找en0或en1下的ip
//私有静态最终字符串登录\u URL=”http://xxx.xxx.x.x:1234/webservice/login.php";
//在模拟器上测试:
私有静态最终字符串登录\u URL=”http://www.samplephppage.com";
//从真实服务器进行测试:
//私有静态最终字符串登录\u URL=”http://www.yourdomain.com/webservice/login.php";
//php脚本repsonse中的JSON元素ID:
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
user=(EditText)findViewById(R.id.etUsername);
pass=(EditText)findViewById(R.id.etPassword);
animatedgimageview=((animatedgimageview)findViewById(R.id.animatedgimageview));
animatedgimageview.setAnimatedGif(R.raw.animated\u gif\u大,键入.AS\u原样);
//设置输入字段
//设置按钮
mSubmit=(按钮)findviewbyd(R.id.btnLogin);
//注册侦听器
mSubmit.setOnClickListener(这个);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.btnLogin:
新建AttemptLogin().execute();
打破
案例R.id.textView6:
Intent i=新Intent(这是forgotpassword.class);
星触觉(i);
打破
违约:
打破
}
}
类AttemptLogin扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
布尔失败=假;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(login.this);
setMessage(“检查凭据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
//检查成功标签
成功;
字符串etUsername=user.getText().toString();
字符串etPassword=pass.getText().toString();
试一试{
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,etUsername));
添加(新的BasicNameValuePair(“密码”,etPassword));
Log.d(“请求!”,“启动”);
//通过发出HTTP请求获取产品详细信息
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
//检查日志中的json响应
**Log.d(“登录尝试”,json.toString());***因为:
private static final String LOGIN_URL = "my php page";
不是有效的URL
请阅读该行上方的评论,以获取关于您的登录URL应分配到哪个位置的建议。确保您的登录URL+参数具有逻辑意义。将其记录在您的代码中,然后在浏览器中执行post请求,以查看请求是否实际返回了您想要的JSON。只需检查您的web服务是否执行了entation通过点击任何浏览器中的LOGIN\u URL
以JSON提供响应。
如果有响应,请在应用程序中尝试,否则请检查web服务中的错误代码太多,只添加相关代码您是否检查了来自LOGIN\u URL的响应
?它是有效的JSON吗?它是否包含无效字符?您的应用程序是否具有internet权限?这只是我的问题iss的安全占位符ue不是代码,而是我与我的网络主机之间的代码-谢谢
private static final String LOGIN_URL = "my php page";