gpt4 book ai didi

php - 如何修复空对象引用上的 java.lang.String org.json.JSONObject.toString()'?

转载 作者:行者123 更新时间:2023-11-29 20:31:19 26 4
gpt4 key购买 nike

我的 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));
}
?>

我似乎可以弄清楚为什么它会给我这个异常?

最佳答案

只需在任何浏览器中点击 LOGIN_URL 即可检查您的 Web 服务实现是否以 JSON 格式提供响应。如果响应出现,那么也可以在您的应用程序中尝试,否则请检查您的网络服务是否有错误

关于php - 如何修复空对象引用上的 java.lang.String org.json.JSONObject.toString()'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31905882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com