2015-11-01 68 views
0

我想让我的Android应用程序建立到网站的连接,并通过代码在后台导航网站。据我了解,第一步是让我想浏览该网站,我设法通过这个代码做的HTML源代码:使用Http POST和GET导航网站

public class HttpTest extends Activity { 
private TextView tvCode; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.http_layout); 

    tvCode = (TextView)findViewById(R.id.tvHTMLCode); 
    String s = null; 
    try { 
     GetHtmlSourceCode html = new GetHtmlSourceCode(); 
     html.execute("http://www.youtube.com"); 
     s = html.get(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     tvCode.setText("Error"); 
    } 
    if (s != null) 
     tvCode.setText(s); 

} 

private class GetHtmlSourceCode extends AsyncTask<String, Integer, String> { 
    @Override 
    protected String doInBackground(String... params) { 
     URL url = null; 
     HttpURLConnection conn = null; 
     String content = ""; 

     try { 
      url = new URL(params[0]); 
      conn = (HttpURLConnection)url.openConnection(); 
      InputStream in = conn.getInputStream(); 

      int data = in.read(); 
      while (data != -1) { 
       char c = (char) data; 
       data = in.read(); 
       content += c; 
      } 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
      return "error"; 
     } 
     finally { 
      conn.disconnect(); 
      return content; 
     } 
    } 
} 

}

(YouTube是仅仅作为一个例子)。
从youtube.com获取源代码后,我希望我的应用在搜索框中输入内容,然后单击搜索按钮。
根据我的理解,我需要发送一个POST请求到YouTube以填充搜索框,另一个POST按钮,最后是一个GET,以获得搜索结果页面的html源代码。然而,对这些问题似乎已经解决的HttpClient和HttpPost类的弃用,我有限的英语词汇以及对该主题的一般无知使我很难自己找到解决方案。
有人可以帮忙吗?

+0

如果你想下载的网站整个网页和链接,我建议你拿看看** webzip **。 – Soley

+0

@Salivan我不想下载整个页面。我只想通过代码浏览他们。 – Steyiak

回答

0

尝试使用下面的代码

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 

import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.StatusLine; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.os.AsyncTask; 

public class Request extends AsyncTask<String, String, String>{ 

    private HttpClient httpclient = new DefaultHttpClient(); 
    private HttpResponse response; 
    private String responseString ; 
    @Override 
    protected String doInBackground(String... uri) { 


     StatusLine statusLine = response.getStatusLine(); 
     try { 
      response = httpclient.execute(new HttpGet(uri[0])); 
      statusLine = response.getStatusLine(); 
      if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       responseString = out.toString(); 
       out.close(); 
      } else{ 

       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (ClientProtocolException e) { 

     } catch (IOException e) { 

     } 
     return responseString; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     //TO Do 
    } 
} 

为了请求,请使用以下代码

new RequestTask().execute("http://yourwebsite.com"); 
+0

它无法解析任何Apache导入。 – Steyiak