使用Http POST和GET来浏览网站

时间:2015-11-01 13:11:43

标签: java android

我希望我的Android应用程序建立与网站的连接,并通过代码在后台导航网站。据我所知,第一步是获取我想通过此代码进行导航的网站的html源代码:

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获取源代码后,我希望我的应用程序在搜索框中输入内容,然后单击搜索按钮。
根据我的理解,我需要向youtube发送POST请求以填充搜索框,另一个POST按下按钮,最后获取GET以获取具有搜索结果的页面的html源代码。然而,对于这些问题似乎已经解决的HttpClient和HttpPost类的弃用,我有限的英语词汇以及我对该主题的一般无知使得我自己很难找到解决方案。
有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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");