0

在遵循在线教程后,我能够从 twitter 为我的 android 应用程序检索一些数据。以下代码有效。我基本上只是想构建一个可以检索数据的应用程序,例如暗黑破坏神 3 字符级别。我该怎么做呢?我想我必须使用这个 URL 来检索数据http://us.battle.net/api/d3/profile/Fauntleroy-1134/但是我没有运气。

public class HttpExample extends Activity {

TextView httpStuff;
HttpClient client;
JSONObject json;

final static String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpex);
    httpStuff = (TextView) findViewById(R.id.tvHttp);   
    client = new DefaultHttpClient();
    new Read().execute("created_at");
}

public JSONObject lastTweet(String username) throws ClientProtocolException, IOException, JSONException{
    StringBuilder url = new StringBuilder(URL);
    url.append(username);

    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if (status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);
        return last;
    }else{
        Toast.makeText(HttpExample.this, "error", Toast.LENGTH_SHORT);
        return null;
    }
}

public class Read extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try{
            json = lastTweet("");
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        httpStuff.setText(result);
    }

}

}

4

1 回答 1

0

您从此处检索的数据存储在一个数组中,因此当您解析数据时,

JSONArray timeline = new JSONArray(data);

您将获得 JSON数组Array
的每个元素都是一个Object,因此您可以通过阅读此处的 json 字符串来获得它,您会注意到该字符串以“{”开头,因此根元素不是 Array 而是 Object。

JSONObject last = timeline.getJSONObject(0);

您需要更改解析代码,请尝试以下操作:

// test json string
String data = "{\"heroes\" : [ {\"name\" : \"UUUGUUUUUUUU\",\"id\" : 92352,\"level\" : 60,\"hardcore\" : false,\"paragonLevel\" : 0,\"gender\" : 0,\"dead\" : false,\"class\" : \"demon-hunter\",\"last-updated\" : 1340492305}, {\"name\" : \"RhubarbVole\",\"id\" : 7531555,\"level\" : 50,\"hardcore\" : false,\"paragonLevel\" : 0,\"gender\" : 1,\"dead\" : false,\"class\" : \"monk\",\"last-updated\" : 1339377909}, {\"name\" : \"VoodooFrodo\",\"id\" : 7698952,\"level\" : 32,\"hardcore\" : false,\"paragonLevel\" : 0,\"gender\" : 0,\"dead\" : false,\"class\" : \"witch-doctor\",\"last-updated\" : 1339376344}, {\"name\" : \"CheeseBird\",\"id\" : 13139301,\"level\" : 7,\"hardcore\" : false,\"paragonLevel\" : 0,\"gender\" : 1,\"dead\" : false,\"class\" : \"wizard\",\"last-updated\" : 1338098485} ]}";

root = new JSONObject(data);
JSONArray heroes = root.getJSONArray("heroes");

List<JSONObject> heroesList = new ArrayList<JSONObject>();

for(int i=0;i<heroes.length();i++){
    JSONObject hero = heroes.getJSONObject(i);          
    heroesList.add(hero);                   
}

//...

JSONObject hero = heroesList.get(x);
int level = hero.getInt("level");       
于 2013-03-16T22:08:02.153 回答