gpt4 book ai didi

java - Android 网络服务客户端 (JSON)

转载 作者:行者123 更新时间:2023-11-30 09:17:34 27 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,这个应用程序是在 glassfish 3.1.2.2 中运行的 Web 服务的客户端,这个 Web 服务使用 xml 和 json 解析器,我在外部测试 de WS 以查看我得到的答案当我从服务器查找 xml 答案时,我得到了 xml 数据,Json 答案也是如此。

问题

Android 客户端在 get 方法中出现问题,我将内容类型设置为 application/json,但 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)

最佳答案

您想设置接受 header 。您可以在这里阅读:http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

“Accept 请求 header 字段可用于指定响应可接受的某些媒体类型。”

get.setHeader("Accept", "application/json");

Content-Type header 用于指示您发送的数据类型。在 GET 请求的情况下,您的请求没有正文。在这种情况下,您不需要设置 Content-Type

关于java - Android 网络服务客户端 (JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18877136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com