在遵循在线教程后,我能够从 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);
}
}
}