0

Android の開発と json コードについて、毎日少しずつ学んでいます。しかし今、私はこれに行き詰まっています。オンライン データベースから値を取得して表示できますが、json コード全体が表示されます。そして見せたい部分だけを見せたい。

これは私のコードです。本当に基本的なものだと思いますが、私も学んでいます:)

ご覧のとおり、Webページから値を取得してテキストビューに入れているだけですが、JSONObjectまたはJSONArrayに入れたいのですが、どちらが優れているかわかりません。

誰かがこれを手伝ってくれますか?

敬具

public class Bordje extends Activity{

public void onCreate (Bundle savedInstanceState) {
    try
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bordje);

        //This is out textview element, obtained by id from XML Layout
        TextView myListView = (TextView)findViewById(R.id.netResult2);

        //Lets connect to the internet
        try {
            String result = "";
            //create new client object
            HttpClient httpclient = new DefaultHttpClient();
            //now post to the url
            HttpPost httppost = new HttpPost("http://www.wvvzondag2.nl/android/leesbordje.php");
            //execute url
            HttpResponse response = httpclient.execute(httppost);
            //get message from the response
            HttpEntity entity = response.getEntity();
            //get the content from message
            InputStream webs = entity.getContent();

            //convert response to string
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                //slow our inputstream
                webs.close();
                //puts the resut into a string
                result=sb.toString();
            }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
            }

            //Parsing the JSON Data
            try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        json_data.getString("introtext");
                        //Get an output to the screen
                        //then here should be some code that displays text?
                        //myListView.setText(Html.fromHtml(json_data)); ?
                }

            }catch(JSONException e){
                    Log.e("log_tag", "Error parsing data "+e.toString());
            }

        }catch(Exception e){
            Log.e("log_tag", "Error in http connection"+e.toString());
        }

    }
    catch (Exception e)
    {
        //this is the line of code that sends a real error message to the log
        Log.e("ERROR", "ERROR IN CODE: " + e.toString());
    }
}
4

2 に答える 2

2

どちらが優れているかという問題ではありません。JSON オブジェクトと JSON 配列は 2 つの異なるものです。

JSON 配列は、(類似した) アイテムの順序付けられたシーケンスです (http://www.json.org/javadoc/org/json/JSONArray.html)。

JSONArray jsonArray = new JSONArray("[JSON TEXT]");
String textToDisplay = jsonArray.getString(index); //return String at index

JSON オブジェクトはマップです (http://www.json.org/javadoc/org/json/JSONObject.html)。

JSONObject jsonObj = new JSONObject("[JSON TEXT]");
String textToDisplay = jsonObj.getString("key"); //returns String value

次に、データを取得したら、前と同じようにテキスト ビューに設定します。

myListView.setText(textToDisplay);
于 2012-09-04T17:07:04.403 に答える
0

サーバーから有効な json を取得している場合は、取得するオブジェクトに応じて単純にJSONArrayまたはを作成できるため、どちらが優れているかを言う意味はありません。JSONObjectただし、あなたの場合、おそらく JSONArray になります。

それを達成するには、gsonを使用して有効な json 文字列をJSONObjectorに変換できますJSONArray

.toJson()および.fromJSON(object)メソッドを使用します。

于 2012-09-04T17:09:51.773 に答える