우선 저는 안드로이드 / 자바에 관해서는 멍청한 사람입니다. 이번 주에 막 조사를 시작했습니다.
나는 인터넷을 검색하고 하루 종일 다른 일을 시도하여 웹 페이지의 내용을 자바의 문자열로 가져 오는 방법을 알아 냈습니다 (webview 없음). 찾을 수있는 모든 것이 더 이상 사용되지 않거나 내 방식을 방해하는 이해 부족입니다. 문서와 모든 것을 읽었지만 작업이 너무 간단 해 보이지만 PHP에서는 필요합니다. 하나의 함수 : file_get_contents ()
나는 보이지 않는 WebView를 사용하여 이것을 할 수 있지만, 그것이 갈 길이 아님을 아는 한, 웹 페이지에 물건을 게시 할 수 있기를 원합니다 (자바 스크립트를 수행하여 할 수는 있지만 webview에 있지만 그래도 갈 길은 아닌 것 같습니다).
누군가가 웹 페이지의 내용을 문자열로 가져 오는 방법에 대한 간단한 예제와 웹 페이지에 무언가를 게시하는 간단한 예제를 줄 수 있습니까 (그리고 응답을 문자열로 검색)
가능한 경우 몇 가지 설명이 있지만 작동하는 예제가 있으면 작동 이유를 알 수 있습니다.
이 문제를 접할 수있는 사람을 위해 다음 코드로 해결했습니다 (원하는 / 필요한 경우 게시물 매개 변수 추가 포함).
private class GetContents extends AsyncTask<String, Void, String> {
protected String doInBackground(String... p) {
String targetURL = p[0];
String urlParameters = p[1];
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
protected void onPostExecute(String result) {
// do something
}
}
그리고 다음과 같이 사용합니다. new GetContents.execute ( " http://example.com ", "a = b");
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다