2012-11-12 3 views
1

http://mascwt.oicp.net:7080/Monitoring/Data/getOrgData/ 이것은 URL 주소입니다. 어떻게 URL 반환 데이터에 액세스합니까? url은 json 데이터를 반환해야합니다. 리턴 데이터에 액세스 할 수 있습니다. 나는 안드로이드 HttpClient를 사용하고 싶다. 어떻게 그럴수있어?android에서 URL 반환 데이터에 액세스하는 방법

+0

이 살펴 보자이 당신을 찾을 수 있습니다 http://stackoverflow.com/questions/5577857/retrieving-json-from-url-on-android을 안드로이드에서 json 페이지를로드하는 방법에 대한 대답 – HashtagMarkus

답변

1
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
new AsyncTask<Void,Void,Void>() { 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     JSONObject jObject = getJSONfromURL("http://mascwt.oicp.net:7080/Monitoring/Data/getOrgData/"); 
     return null; 
    } 
}.execute(); 
} 
    public JSONObject getJSONfromURL(String url) { 

// initialize 
InputStream is = null; 
String result = ""; 
JSONObject jArray = null; 

// http post 
try { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 

} catch (Exception e) { 
    Log.e("log_tag", "Error in http connection " + e.toString()); 
} 

// convert response to string 
try { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(
      is, "iso-8859-1"), 8); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
    is.close(); 
    result = sb.toString(); 
} catch (Exception e) { 
    Log.e("log_tag", "Error converting result " + e.toString()); 
} 

// try parse the string to a JSON object 
try { 
    jArray = new JSONObject(result); 
} catch (JSONException e) { 
    Log.e("log_tag", "Error parsing data " + e.toString()); 
} 

return jArray; 

}

1
public class WebServices { 
public JSONObject RequestUrl(String url) { 
    JSONObject jsonResponse = null; 
    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     Log.v("URL request", "--->" + url); 
     URI uri = new URI(url); 
     HttpGet httpget = new HttpGet(uri); 
     httpget.setHeader("Accept", "application/json"); 
     httpget.setHeader("Content-type", "application/json; charset=utf-8"); 
     HttpResponse response = httpClient.execute(httpget); 
     HttpEntity responseEntity = response.getEntity(); 
     String changeTIDRec = EntityUtils.toString(responseEntity); 
     System.out.println(changeTIDRec); 
     jsonResponse = new JSONObject(changeTIDRec); 
     Log.v("WebService", "Response : " + jsonResponse); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return jsonResponse; 
}} 
관련 문제