由于我是Android开发的新手,我在项目中遇到了问题。代码是......
用于处理webservice代码我从main.java文件调用webinterface类,如下所示。
Main.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
WebServiceInterface webinterface= new WebServiceInterface();
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getuser = username.getText().toString();
getpass = password.getText().toString();
HashMap<String, Object> map = new HashMap<String,Object>();
map.put("username", getuser);
map.put("password", getpass);
webinterface.sendRequest(map);
}
});
在Webinterface类中,我收到以下地图值&amp;将它们转换为字符串&amp;发送到RestClient.class以处理webservice请求..
WebServiceInterface.java
ArrayList<String> ast = new ArrayList<String>();
Collection<Object> strings = map.values();
Iterator iterator = strings.iterator();
while (iterator.hasNext()) {
getuser = (String) iterator.next();
ast.add(getuser);
}
String password = ast.get(0).toString();
String username = ast.get(1).toString();
RestClient client = new RestClient(
"***********************");
client.AddParam("username", username);
client.AddParam("password", password);
try {
client.Execute(RequestMethod.POST);
} catch (Exception e) {
e.printStackTrace();
}
String response = client.getResponse();
/** here getting response & sending that response back to Main.java class**/
Main main = new Main();
main.responseData(responsedata);
这里接收到来自接口类的响应方法后,Main.java类中继续进行以下过程
Main.java
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("response is----^^^^^^^^^^====>>>>>> " + getrespstatus + "_______---->> > " + respdoctorID);
}//end of oncreate...
public void responseData(HashMap<String, Object> responsedata) {
// TODO Auto-generated method stub
System.out.println("response data in main class->>>>>>>> " + responsedata);
Collection<Object> strings = responsedata.values();
Iterator iterator = strings.iterator();
while (iterator.hasNext()) {
getresponse = (String) iterator.next();
ast.add(getresponse);
}
String status = ast.get(0).toString();
try {
JSONObject json = new JSONObject(status);
getrespstatus = json.getString("status");
respdoctorID = json.getString("doctorID");
callmethod(getrespstatus, respdoctorID);
System.out.println("response is----^^^^^^^^^^====>>>>>> " + getrespstatus + "_______---->> > " + respdoctorID);
/* if(getrespstatus == "true")
{
startActivity(new Intent(Main.this,Second.class))
}else
{
//some alert message
}*/
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果响应状态== true,我想开始活动。但在这里我得到错误作为空指针异常.. 而我的另一个疑问是在这里我得到了回应&amp; doctorID。如果我试图在oncreate方法中访问以下值,它显示为空值(简单地说,如果我在日志中打印响应值,在响应数据方法中我得到,如果从oncreate打印获取空值)那么,任何人都可以帮助我...
答案 0 :(得分:2)
您必须在super
中致电onCreate
。我强烈建议您阅读有关活动生命周期的this article。
至于返回响应,最简单的方法是将Main实例添加到WebInterface类作为引用,可能在构造函数中。