我正在开发一个 android 应用程序,此应用程序是在 glassfish 3.1.2.2 中运行的 Web 服务的客户端,此 Web 服务与 xml 和 json 解析器一起使用,我在外部测试 de WS 以查看我得到的答案以及我查看的时间对于来自服务器的 xml 答案,我得到了 xml 数据,Json 答案也是如此。
问题
在我将内容类型设置为 application/json 的 get 方法中,android 客户端出现问题,但 WS 仅响应 xml。
谁能告诉我我在哪里做错了,我会把代码放在最后。
代码:-
private class TareaWSObtener extends AsyncTask<String, Integer, Boolean>{
//CONSTANTES
private static final String CONTENT = "Content-Type"; //content-type
private static final String JSONTYPE = "application/json"; //application/json
private static final String LOGWS = "MovilSecure:WebServices";
private int id;
private String nombre;
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
boolean result = true;
Log.i(LOGWS, "Levantando cliente");
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("http://10.0.2.2:8080/WS-MovilSecure_SandBox_ONLY_/webresources/persona");
//get.setHeader("Content-Type", "application/json");
Log.i(LOGWS, "cliente levantado");
try{
Log.i(LOGWS, "Obteniendo data");
HttpResponse resp = httpClient.execute(get);
String stringRsp = EntityUtils.toString(resp.getEntity());
JSONObject respJSON = new JSONObject(stringRsp);
id = respJSON.getInt("idPersona");
nombre = respJSON.getString("nombre");
Log.i(LOGWS, "Data Obtenida");
}
catch(Exception e){
Log.e(LOGWS, "Problemas al conectar con el WS", e);
result = false;
}
return result;
}
protected void onPostExecute(boolean result){
if(result){Log.i(LOGWS, "data:"+id+nombre);}
}
}
日志:-
09-13 21:52:50.073: I/MovilSecure:WebServices(3155): Levantando cliente
09-13 21:52:50.123: I/MovilSecure:WebServices(3155): cliente levantado
09-13 21:52:50.123: I/MovilSecure:WebServices(3155): Obteniendo data
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): Problemas al conectar con el WS
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at org.json.JSON.typeMismatch(JSON.java:111)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at org.json.JSONObject.<init>(JSONObject.java:158)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at org.json.JSONObject.<init>(JSONObject.java:171)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at com.example.wsclient_movilesecure.MainActivity$TareaWSObtener.doInBackground(MainActivity.java:110)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at com.example.wsclient_movilesecure.MainActivity$TareaWSObtener.doInBackground(MainActivity.java:1)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-13 21:52:50.293: E/MovilSecure:WebServices(3155): at java.lang.Thread.run(Thread.java:841)