我有一个用web api编写的Web服务。我从android发布到Web服务。但遗憾的是我遇到了错误。我确定我的网络服务代码。但我不知道为什么我的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)
答案 0 :(得分:0)
在评论中(再次如此“可接受”): 你的服务器等待“[HttpGet]”,你做了一个httpPost?也许你应该期待一个HttpPost。据我所知,它不可能通过get发布一个对象,你可以参数化并传递该对象。