0

我正在尝试在我的 android 应用程序的 EditText 字段中显示已转换为字符串的 JSONObject。当我运行应用程序时,它加载没有错误,但我的 EditText 字段为空。我也尝试过 System.out.println(name); 但没有打印到控制台,所以我最好的猜测是没有实际存储在那里,我不知道为什么。
这是我的主要文件

package com.apitest.rottentomatoestest;

import android.os.Bundle;    
import java.util.ArrayList;
import java.util.HashMap;    
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;    
import org.json.*;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;    
import android.widget.TextView;
import android.widget.EditText;
import java.util.logging.*;

public class Main extends Activity {        

    private static String url = "http://api.rottentomatoes.com/api/public/v1.0/movies/770672123/cast.json?apikey=3p9ehnhzbxwpd6mk8fnncf67";

    private static final String TAG_CAST = "cast";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_CHARACTERS = "characters";

    EditText display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainlayout);
            JSONArray cast = null; 
            JSONParser jParser = new JSONParser();
            JSONObject jSon = jParser.getJSONFromUrl(url);              
            display = (EditText) findViewById(R.id.display);            

            try
            {
                cast = jSon.getJSONArray(TAG_CAST);                 
                for(int i=0; i < cast.length(); i++){
                    JSONObject c = cast.getJSONObject(i);                       
                    String name = c.getString(TAG_NAME);                        
                    display.setText(name); 
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        catch (Exception e){
            Log.e("ERROR", "ERROR IN CODE" + e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mainlayout, menu);
        return true;
    }
}

这是我的 JSONParser 类

package com.apitest.rottentomatoestest;

import java.io.*;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;

import android.util.Log;

public class JSONParser {

    static InputStream inputStream = null;
    static JSONObject jObject = null;
    static String jSon = "";

    public JSONParser() {
        // TODO Auto-generated constructor stub
    }

    public JSONObject getJSONFromUrl(String url){

        //Make HTTP Request
        try {
            //defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            inputStream = httpEntity.getContent();

        } catch (UnsupportedEncodingException e){
            e.printStackTrace();
        } catch (ClientProtocolException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null){
                stringBuilder.append(line + "\n");
            }
            inputStream.close();
            jSon = stringBuilder.toString();
        } catch (Exception e){
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        //try to parse the string to JSON Object
        try {
            jObject = new JSONObject(jSon);
        } catch (JSONException e){
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        //return JSON String
        return jObject;
    }    
}
4

1 回答 1

0
 cast = jSon.getJSONArray(TAG_CAST);

  for(int i=0; i < cast.length(); i++){
       JSONObject c = cast.getJSONObject(i);

        String name = c.getString(TAG_NAME);

        display.setText(name);
}

你最好检查最后一个值name

于 2012-08-16T03:37:57.603 回答