我發現了一個工作代碼,用於在這裏製作簡單的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