2013-07-08 4 views
0

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); 

가 있어야

+1

더 명확하게 문제가 무엇인지 설명해야한다고 생각합니다. –

+0

해당 코드 –

+0

에 문제가 있습니다. 수정했습니다. –

답변

0

루트가 JSONObject 아닌 JSONArray입니다 ... 전혀 인쇄되지 않습니다 열쇠 "results"

0

첫 블록은 json 객체입니다.

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 
관련 문제