由于我是 android 开发的新手,所以我在项目中遇到了一个问题。代码是..
为了处理 web 服务代码,我从 main.java 文件调用 webinterface 类,如下所示。
主.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 类中,我收到以下地图值并将它们转换为字符串并发送到 RestClient.class 以处理 Web 服务请求。
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 类中正在进行以下过程
主.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,我想开始活动。但是在这里我收到了空指针异常的错误。我的另一个疑问是我得到了响应和医生ID。如果我试图在 oncreate 方法中访问以下值,它显示为空值(简单地说,如果我在日志中打印响应值,在我得到的 responseData 方法中,如果从 oncreate 打印得到空值)所以,任何人都可以帮我解决这个...