开发者

android communicate with webpage, get 502 error

I have a friend who's written a servlet which just serves XML (just XML, no SOAP or etc., Content-Type: text/xml).

Now I'm trying to access it using android. I can access the page fine if I surf to the page with Firefox, but if I access it using my android application I get an HTTP 502 error.

The code I'm using is:

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()));
    aler开发者_运维知识库tDialog.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();
}

And are there any recommended ways to parse the XML? Like if there was a specific tag in there such as title, message, ...


For the connection problem I would suggest the following: It seems like there is a problem with a proxy or with the method you try to connect. Usually it would be a proxy, which is not able to forward the request to the specific server. But in the case, that you can view the site via FireFox it shouldn't be that problem. You could try to use the FireBug Addon to analyze the request onto that server though. Anyway, try this method instead of the HttpGet:

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

Another thing you could try would be, using the HttpGet and play with the setFollowRedirects(true/false) method. Maybe you have to turn it off or explicit on.

For the XML problem, I would suggest this site: http://www.ibm.com/developerworks/xml/library/x-android/ which helped me alot to parse some RSS Feeds. Personally I prefer the SAX Parser on that site (they introduce a few technics).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜