我使用json web服务(POST)在一个带有登录页面的android应用程序上工作。当我尝试登录时崩溃。
这是我的代码:
login = (Button)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = edit_email.getText().toString().trim();
String password = edit_password.getText().toString().trim();
call_api(email, password);
}
});
private void call_api(final String email, final String password) {
AsyncTask<String,String,String> sync=new AsyncTask<String, String, String>() {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.setMessage("Please wait");
pd.show();
}
@Override
protected String doInBackground(String... params) {
HttpClient clent = new DefaultHttpClient();
HttpPost post = new HttpPost("http://steerapps.com/yardin/webservice.php?action=login?email="+email+"&password="+password);
try {
HttpResponse respon = clent.execute(post);
result1 = EntityUtils.toString(respon.getEntity());
JSONObject object = new JSONObject(result1);
String s = object.getJSONObject("response").getString("id");
message = object.getJSONObject("response").getString("message");
Log.e("message", "" + message);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.cancel();
if (message.equalsIgnoreCase("You are login Successfully"))
{
Intent it=new Intent(login_Activity.this,MainActivity.class);
startActivity(it);
Toast.makeText(getApplicationContext(),
"Login Successfully ", Toast.LENGTH_LONG)
.show();
}else {
Toast.makeText(getApplicationContext(),
"Login Failed ", Toast.LENGTH_LONG)
.show();
}
}
};
sync.execute();
}
答案 0 :(得分:0)
返回消息而不是null,您将通过doInBackground()方法向onPostExecute方法返回null,该方法将被&#34; String s&#34;参数,你正在调用super.onPostExecute(s)所以,null参数传递给该方法,所以,只需更改此行
返回null
到
在你的doInBackground方法中返回消息
:)
答案 1 :(得分:0)
将String s
设为全局,然后更改
String s = object.getJSONObject("response").getString("id");
message = object.getJSONObject("response").getString("message");
到
s = object.getString("message");