从服务器发送多项选择问题到android?

时间:2015-10-09 07:43:53

标签: php android json android-asynctask

我正在尝试开发Android应用程序,它将从服务器接收多项选择问题,然后将用户点击的选项发送到服务器。我正在尝试使用json发送多项选择题。我的问题是我无法在android中得到问题和选择。我无法弄清楚如何做到这一点。我怎么能写json和android代码来完成任务。以下是我的代码:

data.json

{"questions":
         [

 {
 "text":"What is the capital city of France?",
 "options":
   [
 "Tokyo",
 "Berlin",
 "Helsinki",
 "Paris"
 ]
 },

 {
 "text":"What is 2 + 2?",
 "options":[
 "3",
 "4",
 "5",
 "6"
 ]
 },

 {
 "text":"How many grams are in a kilogram?",
 "options":[
 "10",
 "100",
 "1,000",
 "10,000"
   ]
 }
]
};

data.php

<?php

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

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

$out =json_encode($data);

echo ($out);

?>

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
                    {
                        Log.i("size","adfasdfa");
                        HttpClient client = new DefaultHttpClient();
                        HttpGet post = new HttpGet("http://192.168.10.120/data.php");
                        HttpResponse res= client.execute(post);
                        HttpEntity entity = res.getEntity();

                        String response = EntityUtils.toString(entity);

                        Log.i("response",response);

                        JSONObject obj = new JSONObject(response);
                        String questions = obj.optString("questions").toString();

                        Log.d("lets c what is formes",questions);


                        JSONArray array = new JSONArray(questions);
                        String[] qns = new String[array.length()];


                        for (int i = 0; i < array.length(); i++)
                        {
                            qns[i]= array.getString(i);
                            Log.i("list of question",qns[i]);
                        }


                    }
                    catch(IOException ex){}

                    catch(JSONException ex){}

                return  null;
            }

            @Override
            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};
}

1 个答案:

答案 0 :(得分:0)

使用此方法从post或post请求中的响应中获取JSON。

 HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));

获取JSON后解析它并使用它以您想要的方式显示。