Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android webrequest简单解决方案_Android_Webrequest - Fatal编程技术网

Android webrequest简单解决方案

Android webrequest简单解决方案,android,webrequest,Android,Webrequest,我想通过一个简单的URL连接到一个web服务器(一个页面),这个URL已经包含了我想要发送的任何参数,比如:www.web-site.com/action.php/userid/42/secondpara/23/,然后获取由该站点生成的页面内容(不会比简单的OK/NOK更糟糕)。我怎样才能做到这一点?我没有找到任何适合我的问题的示例代码或文档 谢谢你的帮助。试试这个: public static void connect(String url) { HttpClient httpcli

我想通过一个简单的URL连接到一个web服务器(一个页面),这个URL已经包含了我想要发送的任何参数,比如:www.web-site.com/action.php/userid/42/secondpara/23/,然后获取由该站点生成的页面内容(不会比简单的OK/NOK更糟糕)。我怎样才能做到这一点?我没有找到任何适合我的问题的示例代码或文档

谢谢你的帮助。

试试这个:

public static void connect(String url)
{

    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url); 

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Praeda",response.getStatusLine().toString());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {

            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);
            // now you have the string representation of the HTML request
            instream.close();
        }


    } catch (Exception e) {}
}

    private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}