안드로이드 개발 질문/답변
(글 수 45,052)
-
//the year data to send
-
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
-
nameValuePairs.add(new BasicNameValuePair("year","1980"));
-
-
//http post
-
try{
-
HttpClient httpclient = new DefaultHttpClient();
-
HttpPost httppost = new HttpPost("http://example.com/getAl<wbr />lPeopleBornAfter.php");
-
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
-
HttpResponse response = httpclient.execute(httppost);
-
HttpEntity entity = response.getEntity();
-
Log.e("log_tag", "Error in http connection "+e.toString());
-
}
-
//convert response to string
-
try{
-
StringBuilder sb = new StringBuilder();
-
while ((line = reader.readLine()) != null) {
-
sb.append(line + "\n");
-
}
-
is.close();
-
-
result=sb.toString();
-
Log.e("log_tag", "Error converting result "+e.toString());
-
}
-
-
//parse json data
-
try{
-
JSONArray jArray = new JSONArray(result);
-
for(int i=0;i<jArray.length();i++){
-
JSONObject json_data = jArray.getJSONObject(i);
-
Log.i("log_tag","id: "+json_data.getInt("id")+
-
", name: "+json_data.getString("name")+
-
", sex: "+json_data.getInt("sex")+
-
", birthyear: "+json_data.getInt("birthyear")
-
);
-
}
-
}
-
}catch(JSONException e){
-
Log.e("log_tag", "Error parsing data "+e.toString());
-
}
http로 통신하는건 알겟는데 도통 로직을 모르겟네요;; ㅠㅠ
- <font face="굴림체"></font>
-
<?php
-
mysql_connect("host","username","password");
-
mysql_select_db("PeopleData");
-
-
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
-
while($e=mysql_fetch_assoc($q))
-
$output[]=$e;
-
-
print(json_encode($output));
-
-
mysql_close();
-
?>
이건 php에서 쓰는거구요
mysql과 연동하는거 같은데 해석을 못하겟습니다. ㅜㅜ
2012.02.01 13:55:22
* 자바소스
NameValuePair는 http 통신할때 파라미터를 전달하기 위해 사용됩니다.
http통신 후에 받아오는 리스폰스의 inputStream에는 요청한 결과에 대한 화면이 존재합니다.
화면이라고 표현했지만 http 텍스트일수도있고 파일 형식일수도 있고 json이나 xml 같은 형식의 데이터 일 것입니다.
inputSteam에서 Reader객체를 이용해 내용 전체를 읽어들이면서 하나의 스트링 형태로 만듭니다.(json 파싱을 위해)
문자열을 json방식으로 파싱하여 값을 뽑아내서 처리합니다.
* 웹소스
요청받은 파라미터들을 기준으로 DB에서 값을 조회한후 화면에 json 형태의 문자열로 출력합니다.




걍 보이는 그대로인데
통신타고 어떤값이 리절트에 담겼고
json으로 xml 파싱하는거고
mysql 연결하고 셀렉트 날리는건데 ㅠㅠ