json 사이트 (이 하나의 SITE)에서 일부 데이터를 가져올 수있는 활동에 LogCat 및 TextView를 인쇄하려면 "long_name웹 사이트에서 매개 변수별로 JSON 데이터 가져 오기
이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);
}
}
}
편집 : 지금 나는 그것이 로그 캣에서 인쇄를 수행하는 활동을 입력하지만 때 모든 인쇄 ", 정말 혼란 스러워요 내가 제대로 설명하면 알지도 못하는, 여기 내 활동 , "long_name"을 가진 이름뿐만 아니라. 그리고 텍스트 뷰에이
JSONObject timeline = new JSONObject(data);
은 다음 아마도 JSONArray
로 추출해야
JSONArray timeline = new JSONArray(data);
가 있어야
이
더 명확하게 문제가 무엇인지 설명해야한다고 생각합니다. –
해당 코드 –
에 문제가 있습니다. 수정했습니다. –