首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提高联系WebService的性能?

问如何提高联系WebService的性能?
EN

Stack Overflow用户
提问于 2013-08-14 07:58:54
回答 1查看 1.2K关注 0票数 2

我有一个与WebService通信的安卓应用程序。我的应用程序将请求发送到用php编写的Web。php方面,只需处理请求和echo输出结果为JSON格式。

虽然传输的数据量很小,但有时在我的应用程序中加载数据需要太长时间。

下面是我的android应用程序中负责发送请求和检索响应的方法

代码语言:javascript
复制
/**
 * Posts data to server.
 * 
 * @param url Server URL
 * @param json {@link JSONObject} to be sent
 * @param tag Tag. used to describe the request
 * @return {@link HttpResponse}
 * @throws IOException
 * @throws ClientProtocolException
 */
public HttpResponse postData(String url, JSONObject json, String tag)
        throws ClientProtocolException, IOException {
    SharedPreferences prefs = context.getSharedPreferences(SUPPORTDESK, Context.MODE_PRIVATE);
    HttpResponse httpResponse = null;

    String jsonString = json.toString();
    String encodedURL = URLEncoder.encode(jsonString, HTTP.UTF_8);

    List<NameValuePair> value = new ArrayList<NameValuePair>();
    value.add(new BasicNameValuePair("username", prefs.getString(USERNAME_KEY, "")));
    value.add(new BasicNameValuePair("password", prefs.getString(PASSWORD_KEY, "")));
    value.add(new BasicNameValuePair("tag", tag));
    value.add(new BasicNameValuePair("data", encodedURL));

    DefaultHttpClient httpClient = new DefaultHttpClient();
    //setTimeout(httpClient, 5000);
    HttpPost request = new HttpPost(url);

    // StringEntity entity = new StringEntity(jsonString);
    // request.setHeader("Content-Type", "application/json");
    // request.setEntity(entity);
    request.setEntity(new UrlEncodedFormEntity(value, HTTP.UTF_8));

    httpResponse = httpClient.execute(request);

    return httpResponse;

}

/**
 * Retrieves String representation of {@link HttpResponse}
 * 
 * @param response {@link HttpResponse}
 * @return <b>String</b> Actual Response from server as string.
 * @throws IOException
 * @throws IllegalStateException
 * @throws UnsupportedEncodingException
 */
public static String getServerResponse(HttpResponse response)
        throws IOException, IllegalStateException,
        UnsupportedEncodingException {

    InputStream is = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,
            HTTP.UTF_8), 100);
    StringBuilder sb = new StringBuilder();
    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }

    is.close();
    String out = URLDecoder.decode(sb.toString().trim(), HTTP.UTF_8).trim();
    int ascii = (int) out.charAt(0);

    return (ascii >= 33 & ascii <= 126)?out:out.substring(1);
}

postData方法将发送请求并等待响应,然后响应将传递给getServerResponse以检索响应的字符串表示形式。当然,所有long-Running代码都封装在AsyncTask类中。

下面是我的web服务的示例方法

代码语言:javascript
复制
function transactionDetail($json) {
    $array = json_decode($json, TRUE);
    $db = new DBHelper();
    $transactionId = $array[$db->JS_RP_TRANSACTION_ID];
    $response = $db->getTransactionDetails($transactionId);
    echo urlencode(json_encode($response));
}

那么,我在Android端上使用的方法是发出请求和检索响应,标准方法,,还是有更好的方法?

如何为我提出的请求设置超时?,您可以在我的代码中看到注释行,我试图设置一个超时,但它破坏了代码。

我所使用的Client/Server side代码的组合是否足够好,还是有更多的标准或改进的方法来实现我不知道的?

EN

回答 1

Stack Overflow用户

发布于 2013-08-14 09:36:35

我正在使用完全相同的代码来更新我的应用程序中的数据库,我用php/echo/json发送大量数据,唯一一次我经历了减速,那就是当互联网速度很低或者服务器变慢的时候。

在升级到一个更好的服务器后,这一操作只需要几秒钟就能完成,当信号强度降低,数据传输速度减慢或拨号速度降低时.

您的php代码和java代码是正确的,我怀疑问题在于您的服务器。你应该做测试,当你的手机是在无线网络上良好的信号强度,以排除网速,并测试你的DB助手在php需要多长时间才能得到结果。如果它的SQL连接而不是Mongo或其他什么东西,它的工作速度应该超过50 its,即使结果是10 000行。(根据我自己的经验)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18226155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档