0

我希望在一个活动中能够从一个 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 它根本不打印......

4

3 回答 3

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
于 2013-07-08T07:55:17.477 回答
0

根是 aJSONObject不是 a JSONArray

JSONArray timeline = new JSONArray(data);

应该

JSONObject timeline = new JSONObject(data);

那么您可能应该JSONArray使用密钥提取"results"

于 2013-07-08T07:39:11.363 回答
0

你的第一个块是 json 对象尝试这样

JSONObject jsonobj= new JSONObject(data);
JSONArray jsonarray= jsonobj.getJsonArray("results");
于 2013-07-08T07:42:47.077 回答