안녕하세요. 정규식을 이용한 웹 파싱을 구현하고 있는데요, 파싱된 문자열에 euc-kr에 없는 문자가 포함되어 있으면 TextView에 넣을 때 튕겨버립니다. String 값은 이러한 형식으로 되어 있습니다. 어떻게 해결해야 될지 막막해서 문의드립니다. (코드가 개판이어도 이해해 주세요. 제대로 배운 적이 없다보니 ㅠㅠ)
파서
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import android.util.Log;
- import net.htmlparser.jericho.Source;
- public class PostListParser {
- ArrayList<HashMap<String, String>> ParsePostList;
- Source source;
- String pageSource;
-
- PostListParser() {
- ParsePostList = new ArrayList<HashMap<String, String>>();
- }
- ArrayList<HashMap<String, String>> ParsePostList(String url) {
- try {
- ParsePostList.clear();
- URL targetURL = new URL(url);
- InputStream is = targetURL.openStream();
- source = new Source(new InputStreamReader(is, "EUC-KR"));
- pageSource = source.toString();
-
- Pattern pattern = Pattern.compile("정규식");
- Matcher matcher = pattern.matcher(pageSource);
-
- while(matcher.find()){
- Log.d("PARSER", "LOOP CALLED");
- String original = matcher.group();
- HashMap map = new HashMap<String, String>();
-
- Pattern url_find = Pattern.compile("정규식");
- Matcher url_matcher = url_find.matcher(original);
- if(url_matcher.find()) map.put("url", url_matcher.group());
- ParsePostList.add(map);
-
- }
-
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- return ParsePostList;
- }
-
- }
ListView 어댑터
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class PostListAdapter extends ArrayAdapter<HashMap<String, String>>{
-
- private View v;
- private ViewHolder holder;
-
- public PostListAdapter(Context context, int resource, List> objects){
- super(context,resource,objects);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup harent){
- if(convertView == null){
- LayoutInflater inflater = LayoutInflater.from(getContext());
- v = inflater.inflate(R.layout.view_list_item, null);
- holder = new ViewHolder();
- holder.subject = (TextView) v.findViewById(R.id.view_list_item_title);
- v.setTag(holder);
- } else {
- v = convertView;
- holder = (ViewHolder) v.getTag();
- }
-
- final HashMap map = getItem(position);
-
- holder.subject.setText(map.get("subject").toString());
- return v;
-
- }
-
- class ViewHolder{
- TextView subject;
- }
- }
메인 액티비티(AsyncTask 부분)
- public class PostListAsync extends AsyncTask<String, String, ArrayList<HashMap<String, String>>>{
-
- @Override
- protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
- Log.d("ASYNCTASK", "CALLED");
- return parser.ParsePostList(pageURL);
- }
-
- @Override
- protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
- adapter = new PostListAdapter(PostList.this, R.layout.view_list_item, result);
- lv.setAdapter(adapter);
- lv.setOverScrollMode(ListView.OVER_SCROLL_ALWAYS);
- adapter.notifyDataSetChanged();
- }