2016-05-04 14 views
0

Ich arbeite an einer Android App. Ich möchte, dass die App zufällig einen Namen von json auswählt. Hier ist die json:Android, zufällig von externen JSON

{ 
 
"user": [ 
 
{ 
 
"id": "001", 
 
"name": "Raj Amal", 
 
"email": "[email protected]" 
 
}, 
 
{ 
 
"id": "002", 
 
"name": "Raj", 
 
"email": "[email protected]" 
 
} 
 
] 
 
}

Und hier ist mein Android-Code:

public class MainActivity extends Activity { 
 
\t TextView uid; 
 
\t TextView name1; 
 
\t TextView email1; 
 
\t Button Btngetdata; 
 
\t 
 
\t //URL to get JSON Array 
 
\t private static String url = "http://weblink/json/index.php"; 
 
\t 
 
\t //JSON Node Names 
 
\t private static final String TAG_USER = "user"; 
 
\t private static final String TAG_ID = "id"; 
 
\t private static final String TAG_NAME = "name"; 
 
\t private static final String TAG_EMAIL = "email"; 
 
\t 
 
\t JSONArray user = null; 
 

 

 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     
 
     setContentView(R.layout.activity_main); 
 
     Btngetdata = (Button)findViewById(R.id.getdata); 
 
     Btngetdata.setOnClickListener(new View.OnClickListener() { 
 
\t \t \t 
 
\t \t \t @Override 
 
\t \t \t public void onClick(View view) { 
 
\t \t   new JSONParse().execute(); 
 

 
\t \t \t \t 
 
\t \t \t } 
 
\t \t }); 
 
     
 
     
 
    } 
 

 

 
    
 
    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
 
    \t private ProgressDialog pDialog; 
 
    \t @Override 
 
     protected void onPreExecute() { 
 
      super.onPreExecute(); 
 
      uid = (TextView)findViewById(R.id.uid); 
 
\t \t \t name1 = (TextView)findViewById(R.id.name); 
 
\t \t \t email1 = (TextView)findViewById(R.id.email); 
 
      pDialog = new ProgressDialog(MainActivity.this); 
 
      pDialog.setMessage("Getting Data ..."); 
 
      pDialog.setIndeterminate(false); 
 
      pDialog.setCancelable(true); 
 
      pDialog.show(); 
 
      
 
    \t } 
 
    \t 
 
    \t @Override 
 
     protected JSONObject doInBackground(String... args) { 
 
    \t \t JSONParser jParser = new JSONParser(); 
 

 
    \t \t // Getting JSON from URL 
 
    \t \t JSONObject json = jParser.getJSONFromUrl(url); 
 
    \t \t return json; 
 
    \t } 
 
    \t @Override 
 
     protected void onPostExecute(JSONObject json) { 
 
    \t \t pDialog.dismiss(); 
 
    \t \t try { 
 
    \t \t \t \t // Getting JSON Array 
 
    \t \t \t \t user = json.getJSONArray(TAG_USER); 
 
    \t \t \t \t JSONObject c = user.getJSONObject(0); 
 
    \t \t \t \t 
 
    \t \t \t \t // Storing JSON item in a Variable 
 
    \t \t \t \t String id = c.getString(TAG_ID); 
 
    \t \t \t \t String name = c.getString(TAG_NAME); 
 
    \t \t \t \t String email = c.getString(TAG_EMAIL); 
 
    \t \t \t \t 
 
    \t \t \t \t 
 
    \t \t \t \t //Set JSON Data in TextView 
 
    \t \t \t \t uid.setText(id); 
 
    \t \t \t \t name1.setText(name); 
 
    \t \t \t \t email1.setText(email); 
 

 
    \t \t \t 
 
    \t \t } catch (JSONException e) { 
 
    \t \t \t e.printStackTrace(); 
 
    \t \t } 
 

 
    \t \t 
 
    \t } 
 
    }`

würde ich, wenn ich die Taste ein zufälliger Name presse Shows und Schleifen.

Bitte helfen Danke

+1

eine Liste mit Namen erstellen und eine Zufallszahl verwenden Namen von der Liste –

+0

Sie auswählen Vielen Dank für Ihre Antwort, ist das, was ich zu tun versuche ich brauche eine zufällige ID auswählen und den Namen mit der Show gleiche ID. Aber ich weiß wirklich nicht, wie man Vielen Dank – user3380879

Antwort

0

Sie Zufallsnummer aus Ihrem Array zuerst versuchen, diese legen Sie Ihre Artikel in Temp Arraylist generieren müssen. Ich bin gerade nur das Hinzufügen Zeichen Namen anstelle von Zeichen

String json="{'abridged_cast': 
[{'name':'JeffBridges','id':'162655890','characters':['JackPrescott']}, 
{'name':'CharlesGrodin','id':'162662571','characters':['FredWilson']}, 
{'name':'JessicaLange','id':'162653068','characters':['Dwan']}, 
{'name':'JohnRandolph','id':'162691889','characters':['Capt.Ross']}, 
{'name':'ReneAuberjonois','id':'162718328','characters':['Bagley']}]}"; 

JSONObject jsonResponse; 
try { 
    temp = new ArrayList<String>(); 
    jsonResponse = new JSONObject(json); 
    JSONArray movies = jsonResponse.getJSONArray("abridged_cast"); // add 
    //user here instead of abridged_cas 
    for(int i=0;i<movies.length();i++){ 
     JSONObject movie = movies.getJSONObject(i); 
     JSONArray characters = movie.getJSONArray("characters"); // replace 
     //name instead of characters 
     for(int j=0;j<characters.length();j++){ 
      temp.add(characters.getString(j)); 
     } 
    } 
    Toast.makeText(this, "Json: "+temp, Toast.LENGTH_LONG).show(); 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

hier ist, dass Sie Namen bekommen zufällig mit Onclick oder was auch immer Sie wollen ersetzen müssen.

Random randomizer = new Random(); 
String RandomName = temp.get(randomizer.nextInt(temp.size())); 
1

ändern JSONObject c = user.getJSONObject(0); von dieser Linie ->

JSONObject c = user.getJSONObject(new Random().nextInt(user.length())); 
+0

Vielen Dank, es funktioniert! – user3380879

+0

Bitte, nehmen Sie meine Antwort an. :) –

0

Schleife durch den Eingang JSON. Sie können Java-Funktion int random = Random.nextInt (n) verwenden. Dies gibt ein zufälliges int im Bereich [0, n-1] zurück.

ArrayList<String> names = new ArrayList<>(); 
JSONArray socialArray = response.getJSONArray(data); 
     for (int i = 0; i < socialArray.length(); i++) { 
      JSONObject currentJSON = socialArray.getJSONObject(i); 
      names.add(currentJSON.getString("name"); 
     } 
final int random = Random.nextInt(names.size() + 1); 
Toast.makeText(this, "Random Name: " + names.get(random), LENGTH.SHORT).show(); 
Verwandte Themen