Android从URL获取文本时始终为空

Android从URL获取文本时始终为空,android,http-request,Android,Http Request,我想发送一个http请求并从该请求中获取一个字符串 我目前正在使用这段代码,但无论我的URL是什么,我总是得到null。有什么问题吗 试一试{ //为所需页面创建URL URL=新URL(“http://hostname:80/index.html” 试试这个 URL url = new URL("http://whatever"); URLConnection urlConnection = url.openConnection(); InputStream in = new Buffered

我想发送一个http请求并从该请求中获取一个字符串

我目前正在使用这段代码,但无论我的URL是什么,我总是得到null。有什么问题吗

试一试{ //为所需页面创建URL URL=新URL(“http://hostname:80/index.html”

试试这个

URL url = new URL("http://whatever");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//read stream here    

尝试使用此代码

此代码对我有效:

httpClient = AndroidHttpClient.newInstance("Android");
        String url = "Your URL";
        try{
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //Don't know what string you are waiting for
                 //do something
            }
        }
        catch(IOException e){
            httpClient.close();
        }

如果您提供了正确的url,代码工作正常。当您尝试在Android上执行此操作时,您是否在AndroidManifest.xml中设置了正确的Internet权限?我已在AndroidManifest.xml中添加了Internet权限。我确实为其提供了正确的url,但每次都为空。事实上,我的url不正确,现在我已更正它,并将其设置为空设置一个异常“java.io.EOFException”。这当然是用我的原始代码测试的。url是This=>需要从该特定url返回的字符串是:YldZbUZNakF3TUE9PQ==我测试了上述内容,但仍然是一样的。事实上,我的url是不正确的,现在我已经更正了它,我得到了一个异常“java.io.EOFException”。这当然是用我的原始代码测试的。我在这一行得到一个错误:“HttpResponse response=httpClient.execute(httpGet);”这些是错误的第一行://字段描述符#9 Lorg/apache/http/conn/ClientConnectionManager;受保护的final org.apache.http.conn.ClientConnectionManager connManager;//字段描述符#11 Lorg/apache/http/conn/routing/HttpRoutePlanner;受保护的final org.apache.http.conn.routing.HttpRoutePlanner;//字段描述符#13 Lorg/apache/http/ConnectionReuseStrategy;protectedfinal org.apache.http.ConnectionReuseStrategy-reuseStrategy;
                String URL = "http://hostname:80/index.html"
                String XML = stringWriter.toString();
                // System.out.println(XML);

                se = new StringEntity(XML, "UTF-8");
                se.setContentType("text/xml;charset=UTF-8");
                HttpPost httppost = new HttpPost(URL);
                httppost.setEntity(se);

                HttpClient httpclient = new DefaultHttpClient();
                response = httpclient.execute(httppost);

                HttpEntity entity = response.getEntity();
httpClient = AndroidHttpClient.newInstance("Android");
        String url = "Your URL";
        try{
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //Don't know what string you are waiting for
                 //do something
            }
        }
        catch(IOException e){
            httpClient.close();
        }