2015-10-09 72 views
1

我想開發的android應用程序將從服務器接收多選題,然後發送用戶點擊到服務器的選擇。我正在嘗試使用json發送多選題。我的問題是,我不能在android中得到問題和選擇。我無法弄清楚如何去做。我怎麼能寫json和android代碼來完成任務。下面是我的代碼:發送從服務器到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}; 
} 

回答

0

使用日是從響應中發送或獲取請求中獲取JSON。

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

得到的JSON解析它並用它來以你想要的方式顯示。

+0

HttpClient已棄用,因此請嘗試使用UrlConnectionManager – Madhu