Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Web服务客户端(JSON)_Java_Android_Xml_Json_Web Services - Fatal编程技术网

Java Android Web服务客户端(JSON)

Java Android Web服务客户端(JSON),java,android,xml,json,web-services,Java,Android,Xml,Json,Web Services,我正在开发一个android应用程序,这个应用程序是运行在glassfish 3.1.2.2中的web服务的客户端,这个web服务使用xml和json解析器,我在外部测试de WS以查看我得到的答案,当我从服务器上查找xml答案时,我得到了xml数据,json答案也是如此 问题 在get方法中,我将内容类型设置为application/json,但只设置WS-only-response-xml 谁能告诉我哪里做错了,我会把代码和日志放在最后 代码:- private class TareaWSO

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

问题

在get方法中,我将内容类型设置为application/json,但只设置WS-only-response-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)
私有类TareaWSObtener扩展异步任务{
//康斯坦茨
私有静态最终字符串CONTENT=“CONTENT Type”;//内容类型
私有静态最终字符串JSONTYPE=“application/json”;//application/json
私有静态最终字符串LOGWS=“MovilSecure:WebServices”;
私有int-id;
私有字符串名称;
@凌驾
受保护的布尔doInBackground(字符串…参数){
//TODO自动生成的方法存根
布尔结果=真;
Log.i(LOGWS,“Levantando客户”);
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet=新的HttpGet(“http://10.0.2.2:8080/WS-MovilSecure_SandBox_ONLY_/webresources/persona”);
//get.setHeader(“内容类型”、“应用程序/json”);
Log.i(LOGWS,“客户levantado”);
试一试{
Log.i(LOGWS,“Obteniendo数据”);
HttpResponse resp=httpClient.execute(get);
字符串stringRsp=EntityUtils.toString(resp.getEntity());
JSONObject respJSON=新的JSONObject(stringRsp);
id=respJSON.getInt(“idPersona”);
nombre=respJSON.getString(“nombre”);
Log.i(LOGWS,“数据获取”);
}
捕获(例外e){
Log.e(LOGWS,“连接问题”,e);
结果=假;
}
返回结果;
}
受保护的void onPostExecute(布尔结果){
如果(结果){Log.i(LOGWS,“数据:+id+nombre);}
}
}
日志:-

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)
09-13 21:52:50.073:I/MovilSecure:WebServices(3155):Levantando客户
09-13 21:52:50.123:I/MovilSecure:WebService(3155):客户levantado
09-13 21:52:50.123:I/MovilSecure:WebServices(3155):获取数据
09-13 21:52:50.293:E/MovilSecure:WebServices(3155):连接问题

09-13 21:52:50.293:E/MovilSecure:WebServices(3155):org.json.JSONException:Value我不知道这是否是一个错误,但请尝试取消注释这一行

 //get.setHeader("Content-Type", "application/json");

您要设置接受标头。您可以在此处阅读:

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


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

您应该为此使用robospice库。我也像你一样使用服务器资源,这是不一致的,当你使用3g或如果你打开smarthphone或平板电脑时,这是不好的响应

总之,这是我的代码和一个简单的示例:

(这是一个很好的做法,有3个意图,如果结束,请关闭连接)

boolean ok=false;
字符串插入器=”http://www.SuperCalifragi.net/api/Registro/Registro";
对于(int i=0;i<3;i++)
{
HttpURLConnection=null;
试一试{
URL=新URL(插入器);
connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(真);
connection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”、“应用程序/json”);
连接。设置连接超时(5000);
Gson Gson=新的Gson();
String input=gson.toJson(用户,registoruarioviewmodel.class);
System.out.println(“enviamos:+input”);
OutputStream os=connection.getOutputStream();
write(input.getBytes());
os.flush();
字符串响应=”;
如果(connection.getResponseCode()!=200){
response=“来自服务器的错误\n\n”;
}否则{
response=“来自服务器的响应\n\n”;
}   
System.out.println(“响应对象:“+response+”-“+connection.getResponseCode());
BufferedReader responseBuffer=新的BufferedReader(新的InputStreamReader((connection.getInputStream()));
字符串输出=”;
while((output=responseBuffer.readLine())!=null){
resultadoRegistro=resultadoRegistro+输出;
}            
con=gson.fromJson(resultadoRegistro,Conexion.class);
System.out.println(“CONTENIDO:+con”);
ok=正确;
}捕获(SocketTimeoutException e){
}捕获(格式错误){
}捕获(IOEX异常){
}捕获(例外e){
}最后{
如果(i>3&&connection!=null){
连接断开();
}如果(确定){
i=10;//断开循环
连接断开();
}
}
}

Accept
header怎么样?谢谢你说得对,我不知道head是怎么回事