如何从JSON获取数据 - 不推荐使用HttpClient

时间:2015-09-07 22:57:57

标签: android json httpclient

我曾经从下面的代码中获取HttpClient数据但现在不推荐使用String responseString = null; try { HttpClient client = new DefaultHttpClient(); URI apiCall = new URI("..."); HttpGet request = new HttpGet(); request.setURI(apiCall); HttpResponse response = client.execute(request); responseString = EntityUtils.toString(response.getEntity()); } catch (Exception e) { Log.e("TAG", "some sort of problem encountered", e); } 我已经不能了。你能帮帮我吗?

var corsWhitelist = [/^(?:.+\.)?domain\.com/, /^(?:.+\.)?otherdomain\.com/];

var corsCheck = function(origin, callback) {
  if (corsWhitelist.some(function(item) {
    return (new RegExp(item).test(origin));
  })) {
    callback(null, true);
  } 
  else {
    callback(null, false);
  }
}

corsCheck('otherdomain.com', function(err, result) {
  console.log('CORS match for otherdomain.com: ' + result);  
});

corsCheck('forbiddendomain.com', function(err, result) {
  console.log('CORS match for forbiddendomain.com: ' + result);  
});

1 个答案:

答案 0 :(得分:0)

使用其他类,如

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
    finally {
     urlConnection.disconnect();
   }
 }