2015-11-01 64 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