gpt4 book ai didi

android - 如何获取 URL 的内容并使用 eclipse 在 android java 应用程序中读取它

转载 作者:太空狗 更新时间:2023-10-29 12:57:10 24 4
gpt4 key购买 nike

//下面的代码工作正常但是阅读了源代码和内容,我只需要阅读内容谢谢你的帮助。//

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();
}

}
}

最佳答案

好吧,如果您只需要内容,为什么不以这种方式简化它:

    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;
}

然后只读流?

关于android - 如何获取 URL 的内容并使用 eclipse 在 android java 应用程序中读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5166143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com