2016-05-04 9 views
0

私はアンドロイドアプリで作業しています。私はアプリがjsonの名前をランダムに選択したいと思う。Android、外部jsonからランダム

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 } 
 
    }`

私はだろうと私はボタンをPRESSEランダムな名前:

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

そして、ここでは私のアンドロイドコードです:ここ は、JSONですショーとループ。

は、あなたが最初の一時ArrayListの中にアイテムを追加するためにこれを試して、あなたの配列 から乱数を生成する必要があなたに

+1

名前のリストを作成し、私はランダムIDを選択して名前を表示する必要があることは私が を行うにしようとしていますものです、あなたの答えをありがとうリスト –

+0

から名前を選択するために、乱数を使用同じID。 しかし、私は本当にどのようにわからない ありがとう – user3380879

答えて

0

ありがとう助けてください。私はちょうどあなただけはランダムにonclickを使用して名前を取得したり、あなたが好きなここであなたが文字

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(); 
} 

の代わりに名前を交換する必要がある文字を追加しています。 JSON入力経由>

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

変更JSONObject c = user.getJSONObject(0);。 java関数int random = Random.nextInt(n)を使用することができます。これは、範囲[0、n-1]のランダムなintを返します。

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(); 
+0

ありがとう、それは動作します! – user3380879

+0

歓迎します、私の答えを受け入れてください。 :) –

0

ループ - この行によって

Random randomizer = new Random(); 
String RandomName = temp.get(randomizer.nextInt(temp.size())); 
関連する問題