android2012.10.11 21:07

private ProgressDialog pd;


void createThreadAndDialog() {//다이알로그 실행함수 쓰레드도 돌고 

pd = ProgressDialog.show(this, getResources().getText(R.string.hello),

getResources().getText(R.string.hello), true, false);

pd.setCancelable(true);

Thread thread = new Thread(new Runnable() {

public void run() {


boolean connettResult = isOnline();

if (connettResult) {

// 페이지접속이 됬는지 true값

// Message msg = handler.obtainMessage();

// handler.sendMessage(msg);


handler.sendMessage(Message.obtain(handler, 1));

} else {

// 페이지접속이 됬는지 false값

// connectAlert();

// pd.dismiss();


handler.sendMessage(Message.obtain(handler, 2));

}

}

});

thread.start();

}


private Handler handler = new Handler() {

public void handleMessage(Message msg) {


switch (msg.what) { // 메시지 처리

case 1:

pd.dismiss();

connectComplete(); // 나는 함수를 호출하였다.

break;

case 2: // 기타 전달인자로 인한 처리도 가능

connectAlert();

pd.dismiss();

break;

case 3:

break;

}

// pd.dismiss();

// connectComplete();


}


};


private void connectAlert() {

AlertDialog.Builder alert = new AlertDialog.Builder(this);


// 제목, 메시지, icon, 버튼

alert.setTitle("경고");

alert.setMessage("접속불가");

alert.setIcon(R.drawable.bus_icon);


// cancel : false = 단말기 back button으로 취소되지 않음.

alert.setCancelable(false);


// yes

alert.setPositiveButton("뒤로가기", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

dialog.dismiss(); // dialog 사라짐

onBackPressed();

}

});


// no

alert.setNegativeButton("재접속", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

createThreadAndDialog();

}

});


alert.show();

}


public boolean isOnline() {

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = cm.getActiveNetworkInfo();

if (netInfo != null && netInfo.isConnected()) {

try {

URL url = new URL(onlineURL);

Log.d("=======", "-----" + onlineURL);

HttpURLConnection urlc = (HttpURLConnection) url

.openConnection();

urlc.setConnectTimeout(3000);

urlc.connect();

int aaa = urlc.getResponseCode();

int bbb = 200;

if (aaa == bbb) {

// 200코드=접속확인코드번호 404코드는 접속에러

return new Boolean(true);

}

} catch (MalformedURLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return false;

}


public void connectComplete() {// 완료 함수 

Log.d("====", onlineURL + RM_IDX + "&rt_start=" + RT_START);

connectJson(onlineURL + RM_IDX + "&rt_start=" + RT_START);


}




///////////


private String connectJson(String url) {

// Create the httpclient

HttpClient httpclient = new DefaultHttpClient();


// Prepare a request object

HttpGet httpget = new HttpGet(url);


// Execute the request

HttpResponse response;


// return string

String returnString = null;


try {


// Open the webpage.

response = httpclient.execute(httpget);


if (response.getStatusLine().getStatusCode() == 200) {

// Connection was established. Get the content.


HttpEntity entity = response.getEntity();

// If the response does not enclose an entity, there is no need

// to worry about connection release


if (entity != null) {

// A Simple JSON Response Read

InputStream instream = entity.getContent();

Log.d("=======", "========" + "나오나1나오나1" + instream);


// Load the requested page converted to a string into a

// JSONObject.

String result = "";

Log.d("=======", "========" + "나오나3" + instream);

ja1 = new JSONArray(convertStreamToString(instream));

Log.d("=======", "========" + "나오나4" + instream);


JSONObject order = ja1.getJSONObject(0);

NAME_LIST = order.getString("NAME_LIST").split("\\,");

LAT_LIST = order.getString("LAT_LIST").split("\\,");

LONG_LIST = order.getString("LONG_LIST").split("\\,");

Log.d("###", NAME_LIST + " | " + NAME_LIST.length);

// final String[] array_RM_NAME = new

// String[NAME_LIST.length];

// final String[] array_LAT_LIST = new

// String[LAT_LIST.length];

// final String[] array_LONG_LIST = new

// String[LONG_LIST.length];

float dism = 600000;


for (int i = 0; i < NAME_LIST.length; i++) {

List<String> filelist_name = Arrays.asList(NAME_LIST);

ar_name = new ArrayList<String>(filelist_name);


List<String> filelist_lat = Arrays.asList(LAT_LIST);

ar_lat = new ArrayList<String>(filelist_lat);


List<String> filelist_long = Arrays.asList(LONG_LIST);

ar_long = new ArrayList<String>(filelist_long);


Log.d("###", NAME_LIST[i] + " | " + LAT_LIST[i] + " | "

+ LONG_LIST[i] + filelist_name);


// ar_lat = ar_lat[i];

// LAT_value = LAT_LIST[i];


if (LONG_LIST.length > 0 && LAT_LIST.length > 0) {

// busLocal = [[CLLocation alloc]

// initWithLatitude:[latCK doubleValue]

// longitude:[longCK doubleValue]];

//

//

// CLLocationDistance dis = [nowS1

// distanceFromLocation:busLocal];


Location locationA = new Location("point A");


locationA.setLatitude(Double

.parseDouble(LAT_LIST[i]));

locationA.setLongitude(Double

.parseDouble(LONG_LIST[i]));


Location locationB = new Location("point B");


locationB.setLatitude(37.5604);

locationB.setLongitude(126.9739);


float distance = locationA.distanceTo(locationB);


Log.d("==========", distance + "||");


if (dism > (float) distance) {

dism = (float) distance;

nowthin = i;

Log.d("==========", i + "||");

}

}


HashMap<String, String> jsonMap = new HashMap<String, String>();

jsonMap.put("NAME_LIST", NAME_LIST[i]);

jsonMap.put("LAT_LIST", LAT_LIST[i]);

jsonMap.put("LONG_LIST", LONG_LIST[i]);

Log.d("=======", "========" + nowthin);

listSEARCH.add(jsonMap);

}


System.out.println("json - " + result);

search(nowthin);

instream.close();

}

} else {

returnString = "Unable to load page - "

+ response.getStatusLine();

}

} catch (IOException ex) {

// thrown by line 80 - getContent();

// Connection was not established

returnString = "Connection failed; " + ex.getMessage();

} catch (JSONException ex) {

// JSON errors

returnString = "JSON failed; " + ex.getMessage();

}

return returnString;

}


private static String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

StringBuilder sb = new StringBuilder();


String line = null;

try {

while ((line = reader.readLine()) != null) {

sb.append(line + "\n");

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return sb.toString();

}

신고
Posted by 레드오이

티스토리 툴바