,因为我是Android开发的新手,所以我在项目中遇到了一个问题。代码是..
用于处理Web服务代码我正在从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请求。
weberviceinterface.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;医生。如果我试图在ongreate方法中访问以下值,则显示为null值(仅说我在日志中打印响应值,在响应方法中,我正在获取的响应值,如果从onCreate中打印出null值,那么任何人都可以帮助我...
您必须在onCreate
中调用super
。我强烈建议阅读有关活动生命周期的这篇文章。
对于返回响应,最简单的方法是将主实例添加到Webinterface类中作为引用,也许是在constructor中。