합쭈기 programming

http connect - json 본문

Java/Android

http connect - json

innocent_k 2015. 4. 13. 10:38
protected boolean requestJson(final String addr, final int method, final String data) {
		
		if (chkHandler())
		{
			new Thread(new Runnable() {
				String strJson = "";
				@Override
				public void run() {
					// TODO Auto-generated method stub
					
					try {
						
						
						
						URL url = new URL(addr);
						
						if (url.getProtocol().toLowerCase().equals("http")) {
			                
							HttpURLConnection httpConn = null;
			                
			                httpConn = (HttpURLConnection) url.openConnection();
			                
			                httpConn.setConnectTimeout(10000);
			                httpConn.setDoOutput(true);
			                httpConn.setDoInput(true);
			                httpConn.setRequestMethod(methodToString(method));
			                
			                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(httpConn.getOutputStream()));
					               
			        		bw.write(data); 
			                bw.flush();
			                bw.close();
			                
			                
			                BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
			              
			                if (httpConn.getResponseCode() == HttpsURLConnection.HTTP_FORBIDDEN) {
			                    return; 
			                }
			                String data = "";
			                
			                while ((data = in.readLine()) != null) {
								if (data != null)
									strJson += data;
							}
			                
			                in.close();
			                
			                httpConn.disconnect();
			                
			                JSONArray jArr = new JSONArray(strJson);
			                Util.sendMessage(handle, msgWhat, jArr);
			               	
						}
				  
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						Util.sendMessage(handle, msgError, e.toString());
					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						Util.sendMessage(handle, msgError, e.toString());
					} catch (FileNotFoundException e) {
						// TODO: handle exception
						Util.sendMessage(handle, msgError, e.toString());
					}
					catch (IOException e) {
						// TODO Auto-generated catch blockJSONArray jArr = new JSONArray(strJson);			
						Util.sendMessage(handle, msgError, e.toString());              
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						try {
							JSONObject jObj = new JSONObject(strJson);
							Util.sendMessage(handle, msgWhat, jObj);
						} catch (JSONException e1) {
							// TODO Auto-generated catch block
							Util.sendMessage(handle, msgError, e1.toString());
						}
						//Util.sendMessage(handle, msgError, e.toString());
					}
				  
				}
			}).start();
		}
		else
		{
			return false;
		} 
		 
		return true;
		
	}

'Java > Android' 카테고리의 다른 글

스크롤 안에 리스트  (0) 2015.04.13
SharedPreferences  (0) 2015.04.13
http connect - 파일 업로드  (0) 2015.04.13
hidden keyboard  (0) 2015.04.13
fading actionbar 소스  (0) 2015.04.07