Apache httppost Android webservice返回大量的XML需要花费很长时间

时间:2015-03-12 09:31:33

标签: java android xml apache web-services

我正在使用apache httppost从服务器中的ASP.NET webservice获取XML数据。

当xml记录的数量低于数千(例如400 xml)时,处理时间非常快,大约10秒,但是当记录数量为数千(例如4000 xml)时,处理时间大约为20分钟,这是疯狂。

它应该是100秒((4000/400)* 10)= a.k.a 1分40秒

我正在尝试使用limit和offset来分割我的查询并将httppost执行放在循环中,但它仍然需要相同的时间

httpParameters = new BasicHttpParams();
httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);       
HttpConnectionParams.setTcpNoDelay(httpParameters, true);


httpclient = new DefaultHttpClient(httpParameters);     
httppost = new HttpPost("http://myipaddress/servicename.asmx/" + wsFunctionName);       
httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (int i = 0 ; i < paramname.length; i ++){
    nameValuePairs.add(new BasicNameValuePair(paramname[i], para[i]));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));           
httpresponse = httpclient.execute(httppost);
HttpEntity entity = httpresponse.getEntity();
contentlen = (int) entity.getContentLength();               
InputStream is = entity.getContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

String line = "";
try {
    while ((line = rd.readLine()) != null) {
        result += line;
        progressvalue += line.length();
        prog = (int) ((progressvalue * 100) / contentlen );
        publishProgress(new Integer[]{prog, in+1});
    }   
} catch (Exception e) {
    Log.e("e", e.toString());
} finally {
    is.close();
    entity.consumeContent();
}

注意:以前我没有使用

httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

代码,但处理时间过长(无差异)

修改

问题不在于连接,但是XML返回的行数是如此之大,因此readLine进程一直持续到行结束。

我将我的XML返回从字符串列表[](有很多换行符)转换为字符串。以前它需要大约20分钟,现在它只有5秒,因为只需要读一行。

编辑2:

测试结果非常棒,

使用常规字符串+ =“字符串”需要14秒,使用StringBuilder只需要2秒钟。

谢谢greenapps

1 个答案:

答案 0 :(得分:0)

问题是大量的字符串连接。

我们应该使用public function addNewSellBookAction(Request $request) { $em = $this->getDoctrine()->getManager(); $serializer = $this->container->get('jms_serializer'); $userId = $this->get('security.token_storage')->getToken()->getUser()->getId(); $content = $request->get('book'); $bookData = json_decode($content, true); $bookData['bookImages']=array(); array_push($bookData['bookImages'],array( 'imageName'=>"Amazon Book Image", 'imageUrl'=>"http://url1.com" )); array_push($bookData['bookImages'],array( 'imageName'=>"Amazon Book Image", 'imageUrl'=>"http://url1.com" )); $bookData['bookSeller']=$userId; $book = new Book(); $bookForm = $this->createForm(new BookType(),$book); var_dump($bookForm->getData()->getBookImages()); $bookForm->submit($bookData); var_dump($book->getBookImages()); if($bookForm->isValid()){ $em->persist($book); $em->flush(); return $this->createJsonResponse('success',array('successTitle'=>"Book Successfully added to sell List")); }else{ $error= $serializer->serialize($bookForm,'json'); return new Response($error,200); } } 而不是普通算子+ =