如何在android中访问url返回数据

如何在android中访问url返回数据,android,Android,这是一个url地址。如何访问url返回数据?url需要返回json数据。 它可以访问返回数据。我想使用android HttpClient。怎么可能呢?@Override @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new AsyncTask<Void,Void,V

这是一个url地址。如何访问url返回数据?url需要返回json数据。 它可以访问返回数据。我想使用android HttpClient。怎么可能呢?

@Override
@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;
创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 新建异步任务(){ @凌驾 受保护的Void doInBackground(Void…参数){ //TODO自动生成的方法存根 JSONObject jObject=getJSONfromURL(“http://mascwt.oicp.net:7080/Monitoring/Data/getOrgData/"); 返回null; } }.execute(); } 公共JSONObject getJSONfromURL(字符串url){ //初始化 InputStream=null; 字符串结果=”; JSONObject jArray=null; //http post 试一试{ HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(url); HttpResponse response=httpclient.execute(httppost); HttpEntity=response.getEntity(); is=entity.getContent(); }捕获(例外e){ e(“Log_标记”,“http连接错误”+e.toString()); } //将响应转换为字符串 试一试{ BufferedReader reader=新的BufferedReader(新的InputStreamReader( is,“iso-8859-1”),8); StringBuilder sb=新的StringBuilder(); 字符串行=null; 而((line=reader.readLine())!=null){ sb.追加(第+行“\n”); } is.close(); 结果=sb.toString(); }捕获(例外e){ Log.e(“Log_标记”,“错误转换结果”+e.toString()); } //尝试将字符串解析为JSON对象 试一试{ jArray=新的JSONObject(结果); }捕获(JSONException e){ Log.e(“Log_标记”,“错误解析数据”+e.toString()); } 返回jArray;
}

看看这个:在这里,您将找到如何在android中加载json页面的答案
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;
}}