Http Get方法返回对象移动

时间:2013-08-01 10:25:50

标签: android html http-get

我是新手,正在开发Android应用。我的应用程序工作正常,直到主机没有将他的网页设置为重定向到其他页面。现在我得到一个HTML页面代码

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>Moved</TITLE>
</HEAD>
<BODY>
<H1>Object Moved</H1> this document may be found <a href="some url">here</a>
</BODY>
</HTML>

尝试conn.setInstanceFollowRedirects(false);但仍然没有用。如果我使用GET方法我得到这个但是使用POST方法我得到null有很多空格。请帮忙。

enter code here

这是我在doinbackground()

中的代码
protected Void doInBackground(String... param) {
try
{
 HttpClient httpclient = new DefaultHttpClient(){
 @Override
  protected RedirectHandler createRedirectHandler() {
    return new DefaultRedirectHandler(){
  @Override
  public boolean isRedirectRequested(HttpResponse response,     HttpContext context) {

                                rep="httpclient redirect";
                                return false;
                            }
                        };
                    }
                };
                   HttpGet httppost = new HttpGet(param[0]);
                    httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,  false);
                   httppost.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
 ResponseHandler<String> responseHandler=new BasicResponseHandler();
                   String responseBody = httpclient.execute(httppost, responseHandler);
                   rep+=responseBody;


            }
 catch (Exception e)
            {
                if(e.getMessage()==null)
                    rep+="No response from server";
                else
                rep+="Error: " + e.getMessage();
            }
}`enter code here`

2 个答案:

答案 0 :(得分:0)

那么......你的问题是什么?如果主机移动文件,那么我们无法解决问题..您应该重定向到已移动的资源并从那里获取信息..

答案 1 :(得分:0)

如果我理解你的需要,我曾经面对这种情况。 情况是您请求某个URL并且响应是HTTP 302临时移动或者那种,但是您需要来自该响应的信息。

试试这个

 final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, false);


    // Create No Redirect HttpClient
    HttpClient httpClient = new DefaultHttpClient(){

        @Override
        protected RedirectHandler createRedirectHandler() {
            return new DefaultRedirectHandler(){
                @Override
                public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
                    Log.d(TAG, "HttpClient Redirect");
                    return false;
                }
            };
        }
    };

    CookieStore cookieStore = new BasicCookieStore();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);


    // Prepare Login Request



    HttpPost postLogin = new HttpPost(URL_LOGIN);

    if(username == null || password == null){
        throw new LoginFailException();
    }


    String params1 = URLEncoder.encode("member[email]","utf-8")+"="+URLEncoder.encode(username,"utf-8");
    String params2 = "&"+URLEncoder.encode("member[crypted_password]","utf-8")+"="+URLEncoder.encode(password,"utf-8");
    String params3 = "&"+URLEncoder.encode("action","utf-8")+"="+URLEncoder.encode("login","utf-8");
    String params4 = "&"+URLEncoder.encode("redirect","utf-8")+"=";




    postLogin.setEntity(new StringEntity(params1+params2+params3+params4));
    postLogin.addHeader("Content-Type", "application/x-www-form-urlencoded");
    postLogin.addHeader("Accept-Encoding", "gzip");
    postLogin.addHeader("User-Agent", "Dalvik/1.4.0 (Linux; U; Android 2.3.7; Full Android on x86 Emulator Build/GINGERBREAD)");



    HttpResponse respLogin = httpClient.execute(postLogin, httpContext);

从此实现中,您可以从重定向响应中读取数据