Android Studio를 이용하여 Naver검색 어플리케이션을 개발하자!
3회차 모각표
Naver검색 어플리케이션 Ui수정 및 권한 지정
package com.example.naversearch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText EditText1;
private Thread mThread;
private String inputLine;
private ArrayList<String> titleList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initEditBox();
initButton();
}
private void initEditBox() {
EditText1 = (EditText) findViewById(R.id.EditBox1);
}
private void initButton() {
/* 검색버튼 생성*/
Button Button_Search = (Button) findViewById(R.id.Button_Search);
Button_Search.setOnClickListener(mOnClickListener);
Button_Search.setText("검색");
/* 이전 페이지 버튼 생성*/
Button Button_Prev = (Button) findViewById(R.id.Button_Prev);
Button_Prev.setOnClickListener(mOnClickListener);
Button_Prev.setText("이전 페이지");
/* 다음 페이지 버튼 생성*/
Button Button_Next = (Button) findViewById(R.id.Button_Next);
Button_Next.setOnClickListener(mOnClickListener);
Button_Next.setText("다음 페이지");
}
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button_Search:
//검색버튼 클릭 => 검색어와 검색유형 읽어옴
String query = EditText1.getText().toString();
System.out.println("------------------------");
System.out.println(query);
SearchNews(query);
System.out.println("------------------------");
System.out.println(inputLine);
parsing();
}
}
};
private void SearchNews(final String searchWord) {
new Thread() {
public void run() {
String clientId = ""; //애플리케이션 클라이언트 아이디값"
String clientSecret = ""; //애플리케이션 클라이언트 시크릿값"
try {
String text = URLEncoder.encode(searchWord, "UTF-8");
String apiURL = "https://openapi.naver.com/v1/search/" + "news" + "?query=" + text + "&display=10" + "&start=1"; // json 결과
URL url = new URL(apiURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("X-Naver-Client-Id", clientId);
con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
int responseCode = con.getResponseCode();
BufferedReader br;
if (responseCode == 200) { // 정상 호출
br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
} else { // 에러 발생
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
inputLine = br.readLine();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/*검색 결과 전처리*/
private void parsing() {
String title;
String link;
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(inputLine);
JSONArray jsonArray = jsonObject.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
title = item.getString("title");
link = item.getString("link");
title = title.replace("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>", "").replace(""", "")
.replace("<", "").replace(">", "").replace("&", "").replace("=", "");
System.out.println(title);
titleList.add((title));
;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/Layer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/EditBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/Button_Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5" />
</LinearLayout>
<LinearLayout
android:id="@+id/Layer3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/ListViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/Layer4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/Button_Prev"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/Button_Next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.naversearch">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NaverSearch">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
'모각코(모여서 각자 코딩)' 카테고리의 다른 글
[2021모각코] 5회차 2021.08.05 (0) | 2021.08.25 |
---|---|
[2021모각코] 4회차 2021.07.29 (0) | 2021.08.25 |
[2021모각코] 2회차 2021.07.15 (0) | 2021.07.15 |
[2021모각코] 1회차 2021.07.07 (0) | 2021.07.07 |
2021 하계 모각코 계획 (0) | 2021.06.24 |