2013-06-08 5 views
0
Ce

est le code que je travaille sur:affichage android objet JSON LISTVIEW

private final static String SERVICE_URI = "http://restwebservice.com/test/Service.svc"; 

StringEntity entity; 
String var; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    callWebService(); 

    Button btn = (Button) findViewById (R.id.button1); 

    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(MainActivity.this, Test.class); 
      startActivity(i); 
     } 
    }); 


} 

public void callWebService() { 

    try { 

      // make web service connection 
      HttpPost request = new HttpPost(SERVICE_URI + "/TestApplication"); 
      request.setHeader("Accept", "application/json"); 
      request.setHeader("Content-type", "application/json"); 
      // Build JSON string 
      JSONStringer TestApp = new JSONStringer().object().key("id") 
         .value("1").key("name").value("manish").key("email") 
         .value("[email protected]").key("country") 
         .value("india").endObject(); 
      entity = new StringEntity(TestApp.toString()); 
      var = EntityUtils.toString(entity); 

      Log.d("****Parameter Input****", "Testing:" + TestApp); 
      request.setEntity(entity); 
      // Send request to WCF service 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpResponse response = httpClient.execute(request); 

      Log.d("WebInvoke", "Saving: " + response.getStatusLine().toString()); 
      // Get the status of web service 
      BufferedReader rd = new BufferedReader(new InputStreamReader(
         response.getEntity().getContent())); 
      // print status in log 
      String line = ""; 
      while ((line = rd.readLine()) != null) { 
       Log.d("****Status Line***", "Webservice: " + line); 
      } 




    } catch (Exception e) { 
      e.printStackTrace(); 
    } 

    } 

Je veux les afficher sur un listview. Avez-vous des tutoriels pour me lancer? Je suis nouveau aux services Web. Merci.

Répondre

1

L'un des tutirials les plus populaires sur les vues de la liste qui peuvent vous aider:

-Ravi's blog

étapes à suivre après l'analyse de votre JSON:

1. Map your json objects to pojo. 
2. Store your pojo in an array list if many are there. 
3. Create a list view with a custom adapter. 
4. update your listview with answer from the pojo's that you have mapped with 
    notifyDatasetChanged 

Vous pouvez utiliser la bibliothèque jackson pour analyser json avec une ligne de code.

//1. Convert Java object to JSON format 
ObjectMapper mapper = new ObjectMapper(); 
mapper.writeValue(new File("c:\\user.json"), user); 

//2. Convert JSON to Java object 
ObjectMapper mapper = new ObjectMapper(); 
User user = mapper.readValue(new File("c:\\user.json"), User.class); 

(suivre this lien pour en savoir plus sur tutoriel mapping objet)

0

ce que je l'ai fait pour générer listview de ma réponse JSON, au fond je l'ai écrit des valeurs de ma réponse jon à l'adaptateur et le mettre à mon liste

try{ 
     jArray = new JSONArray(result); 
     for(int i=0;i<jArray.length();i++){ 
       json_data = jArray.getJSONObject(i); 

       Log.i("log_tag","dealid: "+json_data.getString("deal_id")+ 
         ", hotel name: "+json_data.getString("hotel_name")+ 
         ", location: "+json_data.getString("location")+ 
         ", website: "+json_data.getString("website") 
       ); 

     } 

     json_data=new JSONObject(); 
     String[] data=new String[jArray.length()]; 
     planetList = new ArrayList<String>(); 
      for(int i=0;i<jArray.length();i++) 
      { 
       json_data= jArray.getJSONObject(i); 
       data[i]=json_data.getString("deal_id"); 
       Log.i("log_tag", "string "+data[i]); 
       planetList.addAll(Arrays.asList("Deal "+ (i+1))); 
       listAdapter = new ArrayAdapter<String>(this, R.layout.listrow, planetList); 
       runOnUiThread(new Runnable() { 
        public void run() { 
         list.setAdapter(listAdapter); 
        } 
       }); 
      } 
      list.setOnItemClickListener(new OnItemClickListener() { //where to put this piece of code??? 

       public void onItemClick(AdapterView<?> arg0, View arg1, 
         final int arg2, long arg3) { 

       Intent intent = new Intent(context,Finaldeal.class); 
       intent.putExtra("deal", json_data.toString()); 
       startActivity(intent);          
       } 

      }); 

} 
catch(JSONException e){ 
     Log.e("log_tag", "Error parsing data "+e.toString()); 
} 

vous pouvez également vous reporter à la question posée ici i

passing JSON object to another activity on list item click

Questions connexes