我有一个用 web api 编写的 web 服务。我正在从 android 发布到网络服务。但不幸的是,我遇到了错误。我确信我的网络服务代码。但我不知道为什么我的 android 代码只是不发布值。我的网络服务代码如下。
[HttpGet]
public EmergencyOccurenceModel RegisterEmergency(EmergencyOccurenceModel model){
int result = this.webserviceModules.RegisterEmergencyOccurence(model);
if (result > 0)
model.OccurenceType = "success";
else
model.OccurenceType = "failed";
return model;
}
public class EmergencyOccurenceModel
{
public string OccurenceType { get; set; }
public string Address { get; set; } //This is going to store the location of the accident
public string State { get; set; }
}
然后我的android代码粘贴在下面
首先是发送到 web api 时使用的对象
public class EmergencyOccurenceModel {
private String OccurenceType;
private String Address; //This is going to store the location of the accident
private String State;
public void setOccurenceType(String OccurenceType){
this.OccurenceType = OccurenceType;
}
public String getOccurenceType(){
return OccurenceType;
}
public void setAddress(String Address){
this.Address = Address;
}
public String getAddress(){
return Address;
}
public void setState(String State){
this.State = State;
}
public String getState(){
return State;
}
//This strings below would be used to form the json annotations to collect values from the web service
public static String EmergencyOccurenceModel_OccurenceType="OccurenceType";
public static String EmergencyOccurenceModel_Address = "Address";
public static String EmergencyOccurenceModel_State="State";
}
进行发布的 AnsycTask 类
class ReportEmergencyOccurence extends AsyncTask<String,String,String>{
private Context context = null;
private String OccurenceType = null;
private ProgressDialog progressdialog = null;
private String Address = null;
private String loadMsg = null;
OnTaskCompleted listener = null;
EmergencyOccurenceModel model = null; EmergencyOccurenceModel ReturnValue = null;
public ReportEmergencyOccurence(OnTaskCompleted listener,Context context, String OccurenceType,String loadMsg,String Address){
this.listener = listener;
this.context = context;
this.OccurenceType = OccurenceType;
this.Address = Address;
this.loadMsg = loadMsg;
}
@Override
protected void onPreExecute(){
super.onPreExecute();
progressdialog= new ProgressDialog(context);
progressdialog.setMessage(loadMsg);
progressdialog.show();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
model = new EmergencyOccurenceModel();
model.setOccurenceType(OccurenceType);
if(OccurenceType.equals("Accident"))
model.setAddress(Address);
else
model.setAddress(Address);
model.setState("Lagos");
Log.i("values",model.getAddress());
Log.i("values",model.getOccurenceType());
Log.i("values",model.getState());
JSONHttpClient jsonHttpClient = new JSONHttpClient();
ReturnValue = (EmergencyOccurenceModel)jsonHttpClient.PostObject(RestfulServiceUrl.ReportEmergency, model, EmergencyOccurenceModel.class);
return null;
}
@Override
protected void onPostExecute(String s){
if(ReturnValue != null){
if(ReturnValue.getOccurenceType().equals("success"))
this.listener.onTaskCompleted("success");
}
else{
this.listener.onTaskCompleted("failed");
}
this.progressdialog.dismiss(); //This is going to close the progress dialog
}
}
然后logcat报错信息
09-17 09:22:24.681: W/KeyCharacterMap(448): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-17 09:22:24.801: D/dalvikvm(448): GC_EXTERNAL_ALLOC freed 97K, 46% free 3050K/5575K, external 2284K/2734K, paused 63ms
09-17 09:22:26.771: I/values(448): 13, Ibarapa street, ebute metta west
09-17 09:22:26.771: I/values(448): Accident
09-17 09:22:26.771: I/values(448): Lagos
09-17 09:22:27.861: I/jsonobject(448): org.apache.http.entity.StringEntity@40546520
09-17 09:22:28.051: D/dalvikvm(448): GC_FOR_MALLOC freed 139K, 46% free 3187K/5831K, external 1572K/2071K, paused 42ms
09-17 09:22:28.201: D/AndroidRuntime(448): Shutting down VM
09-17 09:22:28.201: W/dalvikvm(448): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-17 09:22:28.201: E/AndroidRuntime(448): FATAL EXCEPTION: main
09-17 09:22:28.201: E/AndroidRuntime(448): java.lang.NullPointerException
09-17 09:22:28.201: E/AndroidRuntime(448): at com.example.jadehealthcare.EmergencyActivity$ReportEmergencyOccurence.onPostExecute(EmergencyActivity.java:182)
09-17 09:22:28.201: E/AndroidRuntime(448): at com.example.jadehealthcare.EmergencyActivity$ReportEmergencyOccurence.onPostExecute(EmergencyActivity.java:1)
09-17 09:22:28.201: E/AndroidRuntime(448): at android.os.AsyncTask.finish(AsyncTask.java:417)
09-17 09:22:28.201: E/AndroidRuntime(448): at android.os.AsyncTask.access$300(AsyncTask.java:127)
09-17 09:22:28.201: E/AndroidRuntime(448): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
09-17 09:22:28.201: E/AndroidRuntime(448): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 09:22:28.201: E/AndroidRuntime(448): at android.os.Looper.loop(Looper.java:130)
09-17 09:22:28.201: E/AndroidRuntime(448): at android.app.ActivityThread.main(ActivityThread.java:3683)