336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


java에서 post 나 get을 이용해서 웹에있는 파일이나 html 파일을 받아올때 사용하는 함수입니다.

안드로이드에서 같이 사용가능합니다.


public static final String getSourceStr(String urlString, boolean isPost,
			String valuePair, String encodingStyle) {

		String result = null;
		boolean isPass = false;

		HttpURLConnection cnx = null;

		try {

			if (!isPost) {
				if (!valuePair.trim().equals("")) {
					urlString = urlString + "?" + valuePair;
				}
			}

			URL url = new URL(urlString);
			cnx = (HttpURLConnection) url.openConnection();
			cnx.setConnectTimeout(6000);
			cnx.setDoOutput(true);
			cnx.setDoInput(true);
			cnx.setUseCaches(false);
			cnx.setRequestProperty("content-type",
					"application/x-www-form-urlencoded");

			if (isPost) {
				cnx.setRequestMethod("POST");
				OutputStreamWriter outStream = new OutputStreamWriter(
						cnx.getOutputStream(), encodingStyle);
				PrintWriter writer = new PrintWriter(outStream);
				writer.write(valuePair);
				writer.flush();
			} else {
				cnx.setRequestMethod("GET");
			}

			byte[] w = new byte[1024];
			int size = 0;
			InputStream inputStream = cnx.getInputStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			while (true) {
				size = inputStream.read(w);

				if (size <= 0) {
					break;
				}

				out.write(w, 0, size);
			}
			out.close();
			inputStream.close();
			result = new String(out.toByteArray(), encodingStyle);

		} catch (SocketTimeoutException e) {
			System.out.println("error");
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (cnx != null) {
				cnx.disconnect();
			}
		}

		return result;

	}


+ Recent posts