2015년 9월 5일 토요일

Naver Open API 사용하는 법

네이버에서 naver api로 검색하거나 아래 URL로 접속

http://developer.naver.com/wiki/pages/OpenAPI



WebRequestListView

public class WebRequestListView extends FragmentActivity {
 private FragmentManager fm;
 private LoadingDialog loadingDlg;
 private MyItemAdapter adapter;
 private ListView lv;

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE); 
  setContentView(R.layout.web_request_list_view);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

  fm = ((FragmentActivity)this).getSupportFragmentManager();
  lv = (ListView)findViewById(R.id.list);

  loadingDlg = new LoadingDialog();
  loadingDlg.setMsg("loading...");
  loadingDlg.setCancelable(false);
  loadingDlg.setNoBtn(true);

  loadingDlg.show(fm, null);
  retrieveContent();
 }

 private void retrieveContent() {
  new AsyncTask<Void, Void, List<MyItem>>() {
   @Override
   protected List<MyItem> doInBackground(Void... params) {
    return getDom("http://openapi.naver.com/search?key=c1b406b32dbbbbeee5f2a36ddc14067f&query=%EB%A6%AC%EB%B7%B0&display=10&start=1&target=blog&sort=sim");
   }

   @Override
   protected void onPostExecute(List<MyItem> items) {
    loadingDlg.dismiss();

    adapter = new MyItemAdapter(WebRequestListView.this, R.layout.list_view_item_rank, items);
    adapter.setContext(WebRequestListView.this);
    lv.setAdapter(adapter);
   }
  }.execute(null, null, null);
 }

 public class MyItemAdapter extends ArrayAdapter<MyItem>  {
  private List<MyItem> items;
  private Context context;
  private int layoutResource;

  public void setContext(Context c) {
   this.context = c;
  }

  public MyItemAdapter(Context context, int layoutResource, List<MyItem> items) {
   super(context, layoutResource, items);
   this.context = context;
   this.items =  items;
   this.layoutResource = layoutResource;
  }

  @Override        
  public View getView(int position, View convertView, ViewGroup parent) {
   final MyItem myItem = items.get(position);

   if (convertView == null) {
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(layoutResource, null);
   }

   if (myItem != null) {
    TextView content = (TextView) convertView.findViewById(R.id.content);

    if (content != null){                             
     content.setTextSize(20);
     content.setTextColor(Color.BLACK);
     content.setText(myItem.getContent());           
    }          
   }                 
   return convertView;         
  }
 }

 public class MyItem {
  public String content;

  public String getContent() {
   return content;
  }

  public void setContent(String content) {
   this.content = content;
  }
 }

 public List<MyItem> getDom(String webpage) {
  List<MyItem> items = new ArrayList<MyItem>();

  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

  try {
   DocumentBuilder builder = builderFactory.newDocumentBuilder();

   URL url = new URL(webpage);

   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setRequestMethod("POST");
   conn.setDoInput(true);
   conn.setDoOutput(true);

   int resCode = conn.getResponseCode();

   InputStream instream = conn.getInputStream();

   Document document = builder.parse(instream);

   Element docEle = document.getDocumentElement();
   NodeList nodelist = docEle.getElementsByTagName("item");
   if ((nodelist != null) && (nodelist.getLength() > 0)) {
    for (int i = 0; i < nodelist.getLength(); i++) {
     
     Element entry = (Element)nodelist.item(i);
     String title = entry.getElementsByTagName("title").item(0).getTextContent();
     String description = entry.getElementsByTagName("description").item(0).getTextContent();
     
     MyItem item = new MyItem();
     //item.setContent(title);
     item.setContent(description);

     items.add(item);
    }
   }
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  }
  catch (SAXException e) {
   e.printStackTrace();
  }
  catch (MalformedURLException e) {
   e.printStackTrace();
  }
  catch (IOException e) {
   e.printStackTrace();
  }

  return items;
 }
}



댓글 없음:

댓글 쓰기