如何获得一个URL的内容,并使用Eclipse来读它在Android的Java应用程序它在、如何获得、应用程序、内容

2023-09-04 06:00:08 作者:Free and easy 洒脱

//以下code正常工作,但读的源$ C ​​$ C以及内容,我只需要阅读的内容感谢您的帮助.//

 包t.n.e;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.net.MalformedURLException;
进口的java.net.URL;

进口org.xml.sax.Parser;

进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.EditText;



公共类urlgettingproject延伸活动{
    私人的EditText T1;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        T1 =(EditText上)findViewById(R.id.T1);
        StringBuilder的内容=新的StringBuilder();


        尝试 {
            网址URL =新的URL(http://10.0.22.222:8080/SaveName.jsp?first=12&second=12&work=min);
            的BufferedReader在=新的BufferedReader(新的InputStreamReader(url.openStream()));
            字符串str;
            而((海峡= in.readLine())!= NULL){
                content.append(STR +\ N);
                T1.setText(内容);
            }
            附寄();
        }赶上(MalformedURLException异常E){
        }赶上(IOException异常E){
            e.printStackTrace();
        }

    }
}
 

解决方案

好吧,如果你只是需要的内容,你为什么不把它简化这种方式:

 私人的InputStream OpenHttpConnection(字符串strURL)
            抛出IOException异常{
        URLConnection的康恩= NULL;
        的InputStream的InputStream = NULL;
        网址URL =新的URL(strURL);
        康恩= url.openConnection();
        HttpURLConnection的httpConn =(HttpURLConnection类),康涅狄格州;
        httpConn.setRequestMethod(GET);
        httpConn.connect();
        如果(httpConn.getResponse code()== HttpURLConnection.HTTP_OK){
            的InputStream = httpConn.getInputStream();
        }
        返回InputStream的;
    }
 

然后把刚才读取流?

一文看懂 网址,URL,域名,IP地址,DNS,域名解析 主机

//Following code works fine but read's the source code as well as the content, I just need to read the content Thanks for the help.//

package t.n.e;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.xml.sax.Parser;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;



public class urlgettingproject extends Activity {
    private EditText T1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        T1 = (EditText)findViewById(R.id.T1);
        StringBuilder content = new StringBuilder();


        try {
            URL url = new URL("http://10.0.22.222:8080/SaveName.jsp?first=12&second=12&work=min");
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                content.append(str +"\n");
                T1.setText(content);
            }
            in.close();
        } catch (MalformedURLException e){
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

解决方案

Well, if you just need the content, why won't you simplify it this way:

    private InputStream OpenHttpConnection(String strURL)
            throws IOException {
        URLConnection conn = null;
        InputStream inputStream = null;
        URL url = new URL(strURL);
        conn = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
        return inputStream;
    }

And then just read the stream?

 
精彩推荐
图片推荐