【问题标题】:android communicate with webpage, get 502 errorandroid与网页通信,得到502错误
【发布时间】:2010-03-01 08:34:55
【问题描述】:

我有一个朋友编写了一个只提供 XML 服务的 servlet(只提供 XML,没有 SOAP 等,Content-Type: text/xml)。

现在我正在尝试使用 android 访问它。如果我使用 Firefox 浏览该页面,我可以正常访问该页面,但如果我使用我的 android 应用程序访问它,我会得到一个 HTTP 502 error

我使用的代码是:

AlertDialog alertDialog;
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("x");
try {
    HttpResponse response = httpClient.execute(httpGet, localContext);
    alertDialog = new AlertDialog.Builder(view.getContext()).create();
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
           return;
        }
    });
    alertDialog.setTitle("Response");
    alertDialog.setMessage(response.getStatusLine().toString());
    alertDialog.show();

    alertDialog.setMessage(EntityUtils.toString(response.getEntity()));
    alertDialog.show();
} catch (IOException e) {
    alertDialog = new AlertDialog.Builder(view.getContext()).create();
    alertDialog.setTitle("Sorry");
    alertDialog.setMessage("There was a problem connecting to the login-service.");
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
             return;
         }
     });
    alertDialog.show();
}

是否有任何推荐的方法来解析 XML?就像那里有一个特定的标签,比如标题,消息,......

【问题讨论】:

    标签: android xml web-services http android-webservice


    【解决方案1】:

    对于连接问题,我建议如下:代理或您尝试连接的方法似乎有问题。通常它是一个代理,它不能将请求转发到特定的服务器。但在这种情况下,您可以通过 FireFox 查看该站点,这应该不是问题。您可以尝试使用 FireBug 插件来分析对该服务器的请求。 不管怎样,试试这个方法而不是HttpGet:

    HttpReq request = Http.getInstance().createRequest();
    request.setUrl("URL");
    request.setMethod("GET");
    request.execute();
    message = request.getResult();
    

    您可以尝试的另一件事是,使用 HttpGet 并使用 setFollowRedirects(true/false) 方法。也许您必须将其关闭或明确打开。

    对于 XML 问题,我建议使用这个站点:http://www.ibm.com/developerworks/xml/library/x-android/,它帮助我解析了一些 RSS 提要。我个人更喜欢那个网站上的 SAX Parser(他们介绍了一些技术)。

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2022-01-17
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 2018-11-04
      • 2017-03-28
      • 1970-01-01
      相关资源
      最近更新 更多