我希望在一个活动中能够从json站点获取一些数据,这个SITE,并且能够在LogCat和TextView中打印只有那些带有“long_name”的城市,我是真的很困惑,甚至不知道我是否正确解释,这是我的活动:
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
public class JSonActivity extends Activity {
TextView tvJSON;
HttpClient klient;
JSONObject json;
final static String adres = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=Empire%20State%20Building&";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
tvJSON = (TextView) findViewById(R.id.tvJSON);
klient = new DefaultHttpClient();
new Gradovete().execute("long_name");
}
public JSONObject getStuff(String town) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(adres);
HttpGet hg = new HttpGet(url.toString());
HttpResponse hr = klient.execute(hg);
int status = hr.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity he = hr.getEntity();
String data = EntityUtils.toString(he);
JSONArray timeline = new JSONArray(data);
JSONObject grads = timeline.getJSONObject(4);
return grads;
} else {
Toast.makeText(JSonActivity.this, "error", Toast.LENGTH_SHORT).show();
return null;
}
}
public class Gradovete extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
String result = null;
try {
json = getStuff("long_name");
return json.getString(params[1]);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
//JSONObject json1 = new JSONObject(result);
//JSONArray jsona = new JSONArray(result);
tvJSON.setText(result);
Log.v("BLAH", result);
}
}
}
编辑:现在当我进入活动时它会在LogCat中打印,但是它会打印所有内容,而不仅仅是那些带有“long_name”的名称。而对于TextView,它根本不打印......
答案 0 :(得分:0)
根是JSONObject
而不是JSONArray
,
JSONArray timeline = new JSONArray(data);
应该是
JSONObject timeline = new JSONObject(data);
然后您可能应该使用密钥JSONArray
"results"
答案 1 :(得分:0)
你的第一个块是json对象尝试那样
JSONObject jsonobj= new JSONObject(data);
JSONArray jsonarray= jsonobj.getJsonArray("results");
答案 2 :(得分:0)
1)首先在JSONObject中转换您的响应。
try {
JSONObject jsonResult=new JSONObject(<your response>);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
可能是你的情况下的字符串;
2)然后从中获取对象数组
JSONArray arrayOfObjects = jsonResult.getJSONArray("results");
3)检查数组的长度并获取每个对象的“address_components”数组。
for(int i=0; i<arrayOfObjects ; i++)
{
// individual object
JSONObject eachObject=arrayOfObjects.getJSONObject(i);
// get "address_components" array
JSONArray resultantArray= eachObject.getJSONArray("address_components");
for(int j=0; j<resultantArray; j++)
{
JSONObject resultObject=resultantArray.getJSONObject(j);
String longName = resultObject.getString("long_name");
TextView.setText(longName);
}//close
}//close