2016-06-14 5 views
-4

Wie bekomme ich insgesamt keine Anzahl von JSON-Arrays? Ich möchte in ID-Datei von ID bekommen, wir können sehen, es gibt zwei ID. Also sollte kein Zählwert zwei sein.Wie bekomme ich insgesamt keine Anzahl von JSON-Arrays?

Gesamtanzahl der Zählung sollte zwei anzeigen. Weil in JSON zwei ID vorhanden ist. Wie können wir diese Zählung bekommen? siehe auf dem Bild, was ich bekomme JSON. Aber ich möchte nur insgesamt keine Anzahl von "ID", die zwei nach dem Klicken auf die Schaltfläche "Leads" ist. lead.json

[ 
     { 
       "id": "449876", 
       "First Name": "Govind", 
"Middle Name" :"Shripatrao", 
"Last Name":"Suryawanshi", 
"City":"Gurgaon", 
"Country":"India", 
"Contact No":"+91 8586925935", 
       "email": "[email protected]", 
"Budget":"Starting at ?1.7 Crores onwards", 
"Project":" -", 
"App Platform":"UtilityApp-Android", 
"Source":"Organic", 
"Campaign":"NA", 
"Lead Time":"11/8/2015 2:51:32", 
"IP Address":"182.64.13.180" 

     }, 
     { 
"id": "425676", 
       "First Name": "Karan", 
"Middle Name" :"Singh", 
"Last Name":"Rana", 
"City":"Chandigarh", 
"Country":"India", 
"Contact No":"+91 9854563132", 
       "email": "[email protected]", 
"Budget":"Starting at ?3.35 Crore onwards", 
"Project":" Myst", 
"App Platform":"UtilityApp-Android", 
"Source":"Organic", 
"Campaign":"NA", 
"Lead Time":"9/15/2015 12:05:28", 
"IP Address":"182.71.22.178" 

       } 

    ] 

Hauptaktivität

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.JsonArrayRequest; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import tatahousingleads.com.tatahousingleads.app.AppController; 


public class MainActivity extends Activity { 



    // json array response url 
    private String urlJsonArry = "http://milagro.in/wip/apps/n/lead.json"; 

    private static String TAG = MainActivity.class.getSimpleName(); 
    private Button btnMakeObjectRequest, btnMakeArrayRequest; 

    // Progress dialog 
    private ProgressDialog pDialog; 

    private TextView txtResponse; 

    // temporary string to show the parsed response 
    private String jsonResponse; 

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

     // btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest); 
     btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest); 
     txtResponse = (TextView) findViewById(R.id.txtResponse); 

     pDialog = new ProgressDialog(this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 



     btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // making json array request 
       makeJsonArrayRequest(); 
      } 
     }); 

    } 







    private void makeJsonArrayRequest() { 

     showpDialog(); 

     JsonArrayRequest req = new JsonArrayRequest(urlJsonArry, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 

         try { 
          // Parsing json array response 
          // loop through each json object 
          jsonResponse = ""; 
          for (int i = 0; i < response.length(); i++) { 

           JSONObject lead = (JSONObject) response 
             .get(i); 

           String id = lead.getString("id"); 
           String firstname = lead.getString("First Name"); 

           String middname = lead.getString("Middle Name"); 
           String lanme = lead.getString("Last Name"); 
           String city =lead.getString("City"); 
           String country =lead.getString("Country"); 
           String contactno =lead.getString("Contact No"); 
           String email = lead.getString("email"); 
           String budget = lead.getString("Budget"); 
           String project = lead.getString("Project"); 
           String appplatform = lead.getString("App Platform"); 


           jsonResponse += "Id: " + id + "\n\n"; 
           jsonResponse += "First Name: " +firstname + "\n\n"; 
           jsonResponse += "Middle Name: " + middname + "\n\n"; 
           jsonResponse += "Last Name: " + lanme + "\n\n"; 
           jsonResponse += "City: " + city + "\n\n"; 
           jsonResponse += "Country: " + country + "\n\n"; 
           jsonResponse += "Contact No: " + contactno + "\n\n"; 
           jsonResponse += "Email: " + email + "\n\n"; 
           jsonResponse += "Budget: " + budget + "\n\n"; 
           jsonResponse += "Project: " + project + "\n\n"; 
           jsonResponse += "App Platform : " + appplatform + "\n\n\n"; 

          } 

          txtResponse.setText(jsonResponse); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
          Toast.makeText(getApplicationContext(), 
            "Error: " + e.getMessage(), 
            Toast.LENGTH_LONG).show(); 
         } 

         hidepDialog(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_SHORT).show(); 
       hidepDialog(); 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(req); 
    } 

    private void showpDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    private void hidepDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 

activity_main.xml

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    android:scrollbarStyle="insideInset" 
    android:scrollbars="vertical" > 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 



    <Button 
     android:id="@+id/btnArrayRequest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="50dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:text="Leads" 
     /> 

    <TextView 
     android:id="@+id/txtResponse" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/btnArrayRequest" 
     android:layout_marginTop="20px" 
     android:padding="20dp" /> 

</LinearLayout> 
    </ScrollView> 

image

+2

JsonArray.length() gibt Ihnen die Länge von JsonArray. –

+0

Bitte genau sein. Erkläre, was du wirklich brauchst. Sie haben bereits response.length() verwendet, um die Länge zu erhalten. – Ayyappan

+0

response.length() alles bereit, geben Sie das. – anonymous

Antwort

1

In Ihrem MainActivity.java Klasse, innerhalb Methode makeJsonArrayRequest(), in OnResponse Callback-Methode, Sie erhalten bereits die Größe des Arrays. Schau dir deine for-Schleife an,

   @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 

         for (int i = 0; i < response.length(); i++) { 

         // Your code 

         } 

         Log.e("Array Length", response.length()); // Here you will get the length.        

Wo Antwort ist Ihr JSONArray.

+0

Bitte json Datei hier sehen Sie können "ID" nur zwei Mal erscheinen. & Ich möchte nach dem Klicken auf Leads-Taste sollte Anzeige Anzahl der ID nur sein, die zwei ist. Momentan erhalte ich den ganzen Json nachdem ich auf "Leads" geklickt habe. – user6313669

+0

Speichern Sie die Array-Länge in einer 'int' Variable und drucken Sie sie dann auf Button Click – Nayan

+1

, weil Sie den Web-Service aufrufen, indem Sie auf 'Leads' klicken. – SripadRaj

Verwandte Themen