想要在微调器中将Json Data与文本和图像绑定

时间:2014-06-25 14:06:01

标签: java android json jsonp

我可以毫无问题地正确获取文本数据,但我不知道如何从Json数据中获取图像。所以,我希望有人改变我的代码。我已经尝试了但是我无法得到它。

// Json数据

 **'c'([{"categoryId":1,"categoryName":"Theater","imagePath":"http://locator.antheminfotech.net/images/icons/minus.png"},{"categoryId":2,"categoryName":"Mall","imagePath":"http://locator.antheminfotech.net/images/icons/minus.png"},{"categoryId":3,"categoryName":"Hospital","imagePath":"http://locator.antheminfotech.net/images/icons/minus.png"},{"categoryId":4,"categoryName":"School","imagePath":"http://locator.antheminfotech.net/images/icons/minus.png"},{"categoryId":5,"categoryName":"College","imagePath":"http://locator.antheminfotech.net/images/icons/minus.png"},{"categoryId":6,"categoryName":"ChemistShop","imagePath":"http://locator.antheminfotech.net/images/icons/minus.png"}]);*

//这是我的主要活动

public class SpinnerJsonFetch extends ActionBarActivity {

Spinner sp ;

TextView categoryId;

TextView CategoryName;

ArrayList<String> oslist = new ArrayList<String>();

//JSON Node Names 
private static final String TAG_OS = "c";
private static final String TAG_ID = "categoryId";
private static final String TAG_NAME = "categoryName";


@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_json_fetch);

    sp = (Spinner)findViewById(R.id.spList);
    //for checking internet connection 
    ConnectionDetector cd = new ConnectionDetector(getApplicationContext());

    if (!cd.isConnectingToInternet()) {

        Toast.makeText(getApplicationContext(), "Please check your internet      connection", Toast.LENGTH_LONG).show();
    }
    else
             //calling GetAsync class 
    new GetAsync().execute();

}
    //GetAsync class for operations 
private class GetAsync extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(SpinnerJsonFetch.this);

        pDialog.setMessage("Getting Data ...");

        pDialog.setIndeterminate(false);

        pDialog.setCancelable(true);

        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(Tags.url);

        System.out.println("Hello");

        return json;
    }

    //on post execute
    @Override
    protected void onPostExecute(JSONObject json) {

        pDialog.dismiss();

        try {

            // Getting Array of Contacts
            JSONArray jsonArray = json.getJSONArray(TAG_OS);

            final String[] items = new String[jsonArray.length()]; 


            // looping through All Contacts
            for(int i = 0; i < jsonArray.length(); i++){

                JSONObject c = jsonArray.getJSONObject(i);

                // Storing each json item in variable
                String name = c.getString(TAG_NAME);

                String id = c.getString(TAG_ID);

                items[i] = name ;


                System.out.println("Hello events "+items);

            }
                    //array adapter to store the items 
            ArrayAdapter<String> adapter = 
                new ArrayAdapter<String> (SpinnerJsonFetch.this, android.R.layout.simple_spinner_item, items);       
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sp.setAdapter(adapter);

        } catch (JSONException e) {

            e.printStackTrace();

        }

    }       

}    

}

我的JSONParsor文件......

try {
        if(json.contains("'")){

            String callback = json.substring(json.indexOf("'") + 1, json.lastIndexOf("'"));
            json= "{ \"" + callback + "\":" + json.substring(json.indexOf("(") + 1, json.lastIndexOf(")")) + "}" ;
        }
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

0 个答案:

没有答案