我是iOS开发的新手。我们的组织有一个Android应用程序,现在我们为iOS开发。 Android,iOS版本中网络调用的响应时间差别很大。这是正常的还是实施故障?登录期间链接的呼叫特别慢。我在iOS中使用NSURLSession进行调用。 对于ios,这是我的代码:
let session = URLSession.init(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
let url:URL = URL.init(string: "https://jsonplaceholder.typicode.com/albums")!
let startDate = DispatchTime.now()
let urlRequest:URLRequest = URLRequest.init(url: url)
session.dataTask(with: urlRequest)
{
data,response,error in
let endDate = DispatchTime.now()
print("For normal call====>\(Double(endDate.uptimeNanoseconds - startDate.uptimeNanoseconds)/1000000000)")
let dataString = String.init(data: data!, encoding: .utf8)
if dataString != nil
{
print(dataString!)
}
}.resume()
响应时间为"正常通话====> 2.045450472"
对于android,这是代码:
private String webUrl="https://jsonplaceholder.typicode.com/albums";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new RequestTask().execute();
}
class RequestTask extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... params)
{
try {
URL url=new URL(webUrl);
long time=System.currentTimeMillis();
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream in = urlConnection.getInputStream();
long end = System.currentTimeMillis();
double dif = end - time;
System.out.print("=============>"+(double)(dif/1000));
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
对于此输出是:&#34; =============&gt; 1.358&#34;
我的网络搜索方法:
func callHttpWebService(request : URLRequest , dataListener : DataProcessorListener? , responseListener : WebResponseListener)
{
//config.timeoutIntervalForRequest=10
let session=URLSession.init(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
let startTime = DispatchTime.now()
let task = session.dataTask(with: request)
{
data, response, error in
let endTime = DispatchTime.now()
print("For \(responseListener)====> \(Double(endTime.uptimeNanoseconds - startTime.uptimeNanoseconds)/1000000000)")
if error != nil
{
DispatchQueue.main.async
{
responseListener.onWebResponseFail()
}
}
guard let httpResponse=response as? HTTPURLResponse else
{
return
}
if httpResponse.statusCode == 200
{
guard let data=data else
{
print("Error in webservice\(error)")
return
}
if ((httpResponse.url?.path.contains("branchImage"))! || (httpResponse.url?.path.contains("image"))! )
{
DispatchQueue.main.async()
{
responseListener.showImageData!(data: data)
}
}
else
{
let dataString = String.init(data: data, encoding: .utf8)
if dataString != nil
{
print(dataString!)
}
if let json = try? JSONSerialization.jsonObject(with: data)
{
if let dataListener=dataListener
{
if dataListener is StudentMarks || dataListener is IndividualAssessmentDataProcessor
{
let mainUrl=response?.url?.absoluteString
dataListener.getUrl!(url: mainUrl!)
}
dataListener.parseData(data: json as AnyObject)
DispatchQueue.main.async()
{
let endTime2 = DispatchTime.now()
print("Time between======>\(Double(endTime2.uptimeNanoseconds - endTime.uptimeNanoseconds)/1000000000)")
responseListener.onWebResponseSuccess(dataListener: dataListener)
}
}
}
else
{
print("Empty Data====>Session Expiry")
//responseListener.sessionExpiry!()
}
}
}
else
{
DispatchQueue.main.async()
{
responseListener.onWebResponseFail()
}
}
}
task.resume()
}