Cómo llamar a un json webservice a través de android

Necesito acceder a un servicio web .Net en formato Rest con JSON. Soy muy nuevo en este concepto y muy confundido acerca de cómo funciona esto …. Cualquiera que pueda dar una visión general de esto. Necesito los pasos que necesito seguir para usar JSON. En este momento mi duda es cómo utilizar JSON para agarrar a la salida.

Esta es la forma más sencilla de analizar el Json web Json

  String str="url"; try{ URL url=new URL(str); URLConnection urlc=url.openConnection(); BufferedReader bfr=new BufferedReader(new InputStreamReader(urlc.getInputStream())); String line; while((line=bfr.readLine())!=null) { JSONArray jsa=new JSONArray(line); for(int i=0;i<jsa.length();i++) { JSONObject jo=(JSONObject)jsa.get(i); title=jo.getString("deal_title"); //tag name "deal_title",will return value that we save in title string des=jo.getString("deal_description"); } } catch(Exeption e){ } 

Mencionar el permiso de Internet en el manifiesto android

La biblioteca de Gson puede analizar su secuencia de json automáticamente al objeto. Ejemplo simple:

  Gson gson = new Gson(); int[] ints = {1, 2, 3, 4, 5}; String[] strings = {"abc", "def", "ghi"}; //(Serialization) gson.toJson(ints); ==> prints [1,2,3,4,5] gson.toJson(strings); ==> prints ["abc", "def", "ghi"] //(Deserialization) int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); ==> ints2 will be same as ints 

Este es el código para la actividad de Android que se lee desde el Servicio Web y analiza el objeto JSON:

 public void clickbutton(View v) { try { // http://androidarabia.net/quran4android/phpserver/connecttoserver.php // Log.i(getClass().getSimpleName(), "send task - start"); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); // HttpParams p = new BasicHttpParams(); // p.setParameter("name", pvo.getName()); p.setParameter("user", "1"); // Instantiate an HttpClient HttpClient httpclient = new DefaultHttpClient(p); String url = "http://10.0.2.2:8080/sample1/" + "webservice1.php?user=1&format=json"; HttpPost httppost = new HttpPost(url); // Instantiate a GET HTTP method try { Log.i(getClass().getSimpleName(), "send task - start"); // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>( 2); nameValuePairs.add(new BasicNameValuePair("user", "1")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httppost, responseHandler); // Parse JSONObject json = new JSONObject(responseBody); JSONArray jArray = json.getJSONArray("posts"); ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < jArray.length(); i++) { HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = jArray.getJSONObject(i); String s = e.getString("post"); JSONObject jObject = new JSONObject(s); map.put("idusers", jObject.getString("idusers")); map.put("UserName", jObject.getString("UserName")); map.put("FullName", jObject.getString("FullName")); mylist.add(map); } Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Log.i(getClass().getSimpleName(), "send task - end"); } catch (Throwable t) { Toast.makeText(this, "Request failed: " + t.toString(), Toast.LENGTH_LONG).show(); } } 

Para obtener más detalles, consulte http://www.codeproject.com/Articles/267023/Send-and-receive-json-entre-android-y-php

usted utiliza los datos de json como sigue:

 var a=new JSONObject(jsonData); 

http://developer.android.com/resources/tutorials/views/hello-mapview.html

Utilice los datos de a para constuct los objetos necesarios y haga lo necesario con el mismo

  • En caché JSON de webservice en android - patrón?
  • Creación de servicio web para aplicaciones de Android
  • ProgressDialog o ProgressBar ralentiza la aplicación que ejecuta Android 4.1
  • ¿Hay alguna forma sólida de hacer frente a la autenticación integrada de Windows (NTLM) desde una aplicación de Android?
  • Bloquear usuarios no Android que llaman al servicio JSON
  • Hacer formas dinámicas (el mejor enfoque)
  • Android: ¿Es una buena idea almacenar Token de autenticación en las preferencias compartidas?
  • Recomendaciones para el formato / protocolo de .NET Web Service para Android, iPhone, etc.
  • ¿Cómo puedo recibir notificaciones múltiples usando GCM sin reemplazar la precedente?
  • Android - Crear WebService RESTful - Recibirlo en el móvil
  • Ksoap2 casting getResponse ()
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.