티스토리 뷰

Programming/안드로이드

HttpCallUtils

파란크리스마스 2013. 5. 3. 16:54
728x90

출처 : android.os.NetworkOnMainThreadException 예외가 발생했을 경우
android.os.NetworkOnMainThreadException
http://aroundck.tistory.com/1240

사용예

        if(android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        
        String test;
		try {
			test = HttpCallUtils.call("aaa", "bbb");
			System.out.println("test = " + test);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

HttpCallUtils

package com.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;

public class HttpCallUtils {

	private static String url = "https://test.com/login_check.php";
	private static String key_user = "mb_id";
	private static String key_password = "mb_password";	
	
	/*
	public static void main(String[] args) throws Exception {
		String test = HttpCallUtils.call("aaa", "bbb");
		System.out.println("test = " + test);
	}
	*/
	
	public static String call(String parma1, String param2) throws Exception {
		
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);

		HttpParams params = client.getParams();
		params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
		params.setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(15000));
		params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(15000));

		ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
		nameValuePairs.add(new BasicNameValuePair(key_user, parma1));
		nameValuePairs.add(new BasicNameValuePair(key_password, param2));
		UrlEncodedFormEntity entityRequest = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
		post.setEntity(entityRequest);
		
		//
		HttpResponse response = client.execute(post);
		
		StringBuffer sb = new StringBuffer();

		// Examine the response status
		System.out.println(response.getStatusLine());

		// Get hold of the response entity
		HttpEntity entity = response.getEntity();

		// If the response does not enclose an entity, there is no need
		// to worry about connection release
		if (entity != null) {
			InputStream instream = entity.getContent();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
				// do something useful with the response
				String line;
				while ((line = reader.readLine()) != null)
					sb.append(line+"\n");
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				instream.close();
			}
		}	
		return sb.toString();
	}	
}

 

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함