在android中检索JSON数据?

时间:2015-10-09 09:33:37

标签: php android json

我正在尝试从我的Android应用程序中的服务器检索JSON数据。 JSON文件包含问题以及4个选项。我试图单独检索问题和4个选项并设置为在android中列出视图以显示多选题。 Android通过响应,但我无法获得个人数据。以下是我的代码。

file.json

 {"multiple":[{
"question": "In which course are you inrolled in?",
"choice 1":"BIM",
"choice 2":"BBA",
"choice 3":"BIT",
"choice 4":"BSCCSIT"
},
{
"question": "What comes after n?",
"choice 1":"s",
"choice 2":"t",
"choice 3":"o",
"choice 4":"p"
}
]
}

sending.php

<?php

    header('Content-type:application/json');

$data =file_get_contents('/var/www/html/file.json');

$row =json_encode($data);

echo ($row);
?>

MainActivity.java

package com.multiple;

import android.app.*;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.*;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



public class MainActivity extends Activity
{

    private ListView listview;
    List<HashMap<String,String>> collect= new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.list);
        populate p = new populate();
        p.execute();
    }

    public class populate extends AsyncTask<Void, Void, Void>
        {
            public Void doInBackground(Void... params)
            {
                    try
                    {

                        HttpClient client = new DefaultHttpClient();
                        HttpGet post = new HttpGet("http://192.168.10.120/sending.php");
                        HttpResponse res= client.execute(post);
                        HttpEntity entity = res.getEntity();

                        String response = EntityUtils.toString(entity);
                        Log.i("response",response);



                        JSONObject obj = new JSONObject(response);


                        JSONArray jsonArray = obj.optJSONArray("multiple");

                        for(int i=0; i < jsonArray.length(); i++)
                        {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            String id = jsonObject.optString("question").toString();
                            String name = jsonObject.optString("choice1").toString();
                            String salary =jsonObject.optString("choice2").toString();
                            String ssalary =jsonObject.optString("choice3").toString();
                            String sssalary =jsonObject.optString("choice4").toString();

                            Log.i("qq",id);
                            Log.i("asdfas",salary);
                            Log.i("asjdfha",name);
                            Log.i("asdfas",salary);
                        }

//                      String questions = obj.optString("question").toString();
//                      String choices=obj.optString("answers").toString();
                    }

                    catch(IOException ex){}

                    catch(JSONException ex){}

                return  null;
            }

            protected void onPostExecute(Void result)
            {
                super.onPostExecute(result);
            }

        }

        String[] str = new String[]{"first","second","third","fourth","fifth"};
        int[] val = new int[]{R.id.textView1,R.id.checkBox1,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4};
}

我如何单独检索JSON数据?/

2 个答案:

答案 0 :(得分:0)

使用ArrayList&gt;()

ArrayList<HashMap<String,String>> arr_list=new ArrayList<HashMap<String,String>>();

arr_list.clear(); //创建并清除数组

JSONObject obj = new JSONObject(response);


                    JSONArray jsonArray = obj.optJSONArray("multiple");
        int count=1;
                    for(int i=0; i < jsonArray.length(); i++)
                    {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
            String id=String.valueOf(count);
                        String ques = jsonObject.optString("question").toString();
                        String c1= jsonObject.optString("choice1").toString();
                        String c2=jsonObject.optString("choice2").toString();
                        String c3=jsonObject.optString("choice3").toString();
                        String c4=jsonObject.optString("choice4").toString();


                   addToArray(id,ques,c1,c2,c3,c4);
                     count++;
                    }

然后..

答案 1 :(得分:0)

您需要保留Collection中的值,ArrayList JSONObject在这种情况下会很方便:

for(int i=0; i < jsonArray.length(); i++){
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    arrListJsonObject.add(jsonObject); 

    /* String id = jsonObject.optString("question").toString();
    String name = jsonObject.optString("choice1").toString();
    String salary =jsonObject.optString("choice2").toString();
    String ssalary =jsonObject.optString("choice3").toString();
    String sssalary =jsonObject.optString("choice4").toString();

    Log.i("qq",id);
    Log.i("asdfas",salary);
    Log.i("asjdfha",name);
    Log.i("asdfas",salary); */
}

此处 arrListJsonObject ArrayList<JSONObject>。 现在你可以使用:

for(int i=0;i<arrListJsonObject.size();i++){
    JSONObject jsonObject = arrListJsonObject.get(i); // or arrListJsonObject.get(position) inside getView method of a custom Adapter.
    String question = jsonObject.optString("question").toString();
    String c1 = jsonObject.optString("choice1").toString();
    String c2 = jsonObject.optString("choice2").toString();
    String c3 = jsonObject.optString("choice3").toString();
    String c4 = jsonObject.optString("choice4").toString();

   // Now you have question and choices as question, c1, c2, c3, c4 respectively.
}

希望能帮助!!!