我发现了一个工作代码,用于在这里制作简单的HTTP请求,从How can I make a simple HTTP request in MainActivity.java? (Android Studio)开始,我将在下面发布它(有一些更改,如果我没有错,现在需要使用try{} catch{}
)。但是我想问一下如何获得内容?我在下面的方式与代码打交道:发出简单的HTTP请求并接收内容
GetUrlContentTask req = new GetUrlContentTask();
req.execute("http://192.168.1.10/?pin=OFF1");
textView3.setText(req.doInBackground("http://192.168.1.10/?pin=OFF1"));
GetUrlContentTask
private class GetUrlContentTask extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... urls) {
// String content1 = "";
try {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String content = "", line;
while ((line = rd.readLine()) != null) {
content += line + "\n";
}
// content1 = content;
}
catch (Exception e) {
e.printStackTrace();
}
// return content1; - returns "", wrong
return "aaa";
//does not work return content;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
// this is executed on the main thread after the process is over
// update your UI here
}
}
你期待什么样的反应概念的证明 - 一个JSON对象? – Barns
我建议你使用[Retrofit](http://square.github.io/retrofit/) – fn5341
@ Barns52我期望纯HTML文本。 – Czarek