JSON对象和数组

时间:2013-07-29 12:25:56

标签: android json

我认为转换JSONArrayJSONObject时遇到问题。 我的PHP看起来像这样

{"list":[{"Name":"Michael","Hour":"one"},{"Name":"Johhny","Hour":"two"}]}.

我正在尝试填充微调器,但它不起作用,它们仍然是空的。我看到了一些类似的问题但没有很好的解释。希望你能帮助我。

public class Choose extends Activity {

Spinner spinner1, spinner2;
InputStream is=null;
String result=null;
String line=null;
String[] Name, Hour;   


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

        spinner1=(Spinner) findViewById(R.id.spinner1);
        spinner2=(Spinner) findViewById(R.id.spinner2);
        final List<String> list1=new ArrayList<String>();
        final List<String> list2=new ArrayList<String>();
        Button b=(Button) findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                try
                {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.0.105/new/spinner.php");
                HttpResponse response = httpclient.execute(httppost);
                Log.e("Fail 1", "3");

                HttpEntity entity = response.getEntity();
                Log.e("Fail 1", "4");

                is = entity.getContent();
                Log.e("Pass 1", "connection success ");
                }
                catch(Exception e)
                {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
                finish();
                } 

                try
                {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
                }
                catch(Exception e)
                {
                Log.e("Fail 2", e.toString());
                }    

                try
                {
                JSONObject jsonObj=new JSONObject(result);
                JSONArray JA=jsonObj.getJSONArray("list");
                JSONObject json= null;
                Name = new String[JA.length()];       
                Hour = new String[JA.length()];

                for(int i=0;i<JA.length();i++)
                {
                json=JA.getJSONObject(i);
                Name[i] = json.getString("Name");
                Hour[i]=json.getString("Hour");
                }
                Toast.makeText(getApplicationContext(), "sss",Toast.LENGTH_LONG).show();

                for(int i=0;i<grad.length;i++)
                {
                list1.add(Name[i]);
                list2.add(Hour[i]);
                }

                spinner_fn();

                }
                catch(Exception e)
                {

                Log.e("Fail 3", e.toString());
                //login.this.finish();

                }
            }
        });
    }

    private void spinner_fn() {
        // TODO Auto-generated method stub

        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
                                        android.R.layout.simple_spinner_item, Name);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(dataAdapter1);


        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(),
                                        android.R.layout.simple_spinner_item, Hour);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter2);


        spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
        public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
        {
        // TODO Auto-generated method stub

        spinner2.setSelection(position);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
        // TODO Auto-generated method stub
        }

        });


        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
        // TODO Auto-generated method stub

        spinner1.setSelection(position);

        }

        public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        }
        });

        } }

这是来自LogCat的报告

07-29 14:41:59.928: D/dalvikvm(15381): GC_CONCURRENT freed 64K, 7% free 12511K/13319K, paused 18ms+13ms, total 63ms
07-29 14:41:59.928: D/dalvikvm(15381): WAIT_FOR_CONCURRENT_GC blocked 27ms
07-29 14:42:00.118: D/libEGL(15381): loaded /system/lib/egl/libEGL_mali.so
07-29 14:42:00.138: D/libEGL(15381): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-29 14:42:00.148: D/libEGL(15381): loaded /system/lib/egl/libGLESv2_mali.so
07-29 14:42:00.153: D/(15381): Device driver API match
07-29 14:42:00.153: D/(15381): Device driver API version: 10
07-29 14:42:00.153: D/(15381): User space API version: 10 
07-29 14:42:00.153: D/(15381): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
07-29 14:42:00.218: D/OpenGLRenderer(15381): Enabling debug mode 0
07-29 14:42:17.183: D/dalvikvm(16000): GC_CONCURRENT freed 64K, 7% free 12511K/13319K, paused 5ms+2ms, total 22ms
07-29 14:42:17.183: D/dalvikvm(16000): WAIT_FOR_CONCURRENT_GC blocked 9ms
07-29 14:42:17.268: D/libEGL(16000): loaded /system/lib/egl/libEGL_mali.so
07-29 14:42:17.278: D/libEGL(16000): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-29 14:42:17.278: D/libEGL(16000): loaded /system/lib/egl/libGLESv2_mali.so
07-29 14:42:17.283: D/(16000): Device driver API match
07-29 14:42:17.283: D/(16000): Device driver API version: 10
07-29 14:42:17.283: D/(16000): User space API version: 10 
07-29 14:42:17.283: D/(16000): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
07-29 14:42:17.313: D/OpenGLRenderer(16000): Enabling debug mode 0
07-29 14:42:31.903: D/dalvikvm(16000): GC_CONCURRENT freed 12K, 6% free 12897K/13703K, paused 13ms+2ms, total 26ms

1 个答案:

答案 0 :(得分:0)

http://vimaltuts.com/android-tutorial-for-beginners/android-spinner-control-example

此链接可能对您有所帮助 Spinner将按照给定示例中的步骤进行弹出。