[HTML] REST or RESTful 이 뭔가요? ( Representational state transfer )
[android/안드로이드] Restful Post sample code
Restful Get 은 Post 와 똑같다. 다만, HttpPost 대신 HttpGet Class 를 사용하면 된다.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost( url );
// Parameters setting.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add( new BasicNameValuePair( key1, value1 ) );
nameValuePairs.add( new BasicNameValuePair( key2, value2 ) );
try{
httpPost.setEntity( new UrlEncodedFormEntity( nameValuePairs ) );
HttpResponse httpResponse = httpClient.execute( httpPost );
HttpEntity entity = httpResponse.getEntity();
if ( entity != null ){
InputStream ins = entity.getContent();
String result = convertStreamToString( ins );
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
JSONObject json = new JSONObject( result );
try{
return json.getString( KEY_ID );
}
catch( Exception e ){
e.printStackTrace();
}
}
}
catch( Exception e ){
e.printStackTrace();
}
return null;
private static String convertStreamToString( InputStream ins ){
BufferedReader reader = new BufferedReader( new InputStreamReader( ins ) );
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}'프로그래밍 놀이터 > 안드로이드, Java' 카테고리의 다른 글
[android/안드로이드] android repository address (0) | 2012.10.04 |
---|---|
[android/안드로이드] layout xml의 tools:context 은 뭔가요? (8) | 2012.10.04 |
[android/안드로이드] camera take & crop 동시에 하기. (0) | 2012.10.04 |
[android/안드로이드] gallery를 통해 pick& crop 동시에 하는 intent 생성. (0) | 2012.10.04 |
[android/안드로이드] string xml에 white space넣는 방법 (2) | 2012.10.04 |
댓글