안드로이드 개발 질문/답변
(글 수 45,052)
post방식을 약간 변형하여 get방식으로 php를 호출 하는데
3G망이나 와이파이 망이 꺼져있을때 생기는 에러는 잡았습니다...
하지만 php에서 검색 결과가 없어서 나는 에러는 여기저기 try catch로 잡아봣는데
못잡겠습니다..ㅠㅠ 로그켓 기록 보더라도 널포인터 만 나고 어느 위치에서 에러나는지 안나오고;;
도움좀 부탁드릴게요;;
public class Nsulib_Serch extends Activity implements OnClickListener {
EditText edittext;
Button button;
String arg;
String[][] cube;
String[] cube1;
String[] item;
String bkna;
char[] chackPoint;
String scresult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.serch);
edittext = (EditText) findViewById(R.id.serch_edit); button = (Button) findViewById(R.id.serch_bt); button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bkna = edittext.getText().toString();
if(bkna.length()<=1){
Toast.makeText(this, "2글자 이상 입력해주세요", Toast.LENGTH_LONG).show();
}
if (bkna.length() >= 2) {// 0607 추가
item = null;
chackPoint = bkna.toCharArray();
if (chackPoint[0] >= 'a' && chackPoint[0] <= 'z'
|| chackPoint[0] >= 'A' && chackPoint[0] <= 'Z') {
try{
HttpPostData();
}catch(Exception e){Toast.makeText(this, "통신 상태가 안좋습니다.", Toast.LENGTH_LONG).show();}
} else {
try {
sendData(bkna);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){Toast.makeText(this, "통신 상태가 안좋습니다.", Toast.LENGTH_LONG).show();}
}
// Toast.makeText(this,item[0],Toast.LENGTH_SHORT).show();
try{
if (item.length == 0) {
Toast.makeText(this,"검색 결과가 없습니다.",Toast.LENGTH_SHORT).show();
} else {// 0607 추가 검색 결과 없을시 에러 처리
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, item);
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(Nsulib_Serch.this,
Nsulib_ChoiceBook.class);
intent.putExtra(Nsulib_ChoiceBook.TEXT_INPUT,
cube1[(int) id]);
startActivity(intent);
}
});
}
}catch(Exception e){Toast.makeText(this, "통신 상태가 안좋습니다.", Toast.LENGTH_LONG).show();}
}
}
private void sendData(String bkna) throws ClientProtocolException,
IOException {
// TODO Auto-generated method stub
HttpPost request = makeHttpPost(bkna,
"http://burningroid.cafe24.com/nsu/index.php");
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> reshandler = new BasicResponseHandler();
try{
cube1 = client.execute(request, reshandler).split("hopalt");
int j = 0;
j = cube1.length;
if(j==0){
Toast.makeText(this, "검색 결과가 없습니다.", Toast.LENGTH_LONG).show();
}else{
cube = new String[j][];
item = new String[j];
for (int i = 0; i < j; i++) {
cube[i] = cube1[i].split(",");
item[i] = cube[i][2];
}}
}catch(Exception e){
Toast.makeText(this, "검색 결과가 없습니다.", Toast.LENGTH_LONG);
}
}
private HttpPost makeHttpPost(String $bkna, String $url)
throws UnsupportedEncodingException {
// TODO Auto-generated method stub
HttpPost request = new HttpPost($url);
Vector<NameValuePair> nameValue = new Vector<NameValuePair>();
nameValue.add(new BasicNameValuePair("bkna", $bkna));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValue,
HTTP.UTF_8);
request.setEntity(ent);
return request;
}
private HttpEntity makeEntity(Vector<NameValuePair> $nameValue) {
HttpEntity result = null;
try {
result = new UrlEncodedFormEntity($nameValue, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
Log.i("json2 : ", e.getMessage());
// Toast.makeText(this,"여기2",Toast.LENGTH_LONG).show();
} return result; }
protected void HttpPostData() {
// TODO Auto-generated method stub
try {
URL url = new URL("
"http://burningroid.cafe24.com/nsu/test1.php?bkna=" + bkna);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setDefaultUseCaches(false);
http.setDoInput(true);
http.setDoOutput(true);
http.setRequestMethod("POST");
http.setRequestProperty("content-type",
"application/x-www-from-urlencoded");
StringBuffer buffer = new StringBuffer();
buffer.append("bkna").append("=").append(bkna);
OutputStreamWriter outStream = new OutputStreamWriter(
http.getOutputStream(), "UTF-8");
PrintWriter writer = new PrintWriter(outStream);
writer.write(buffer.toString());
writer.flush();
InputStreamReader tmp = new InputStreamReader(
http.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(tmp);
StringBuilder builder = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) {
builder.append(str + "hopalt");
}
try{
cube1 = builder.toString().split("hopalt");
int j = 0;
j = cube1.length;
j = j - 1;
if(j==0){
Toast.makeText(this, "검색 결과가 없습니다.", Toast.LENGTH_LONG).show();
}else{
cube = new String[j][];
item = new String[j];
for (int i = 0; i < j; i++) {
cube[i] = cube1[i].split(",");
item[i] = cube[i][2];
}}
}catch(Exception e){
Toast.makeText(this, "검색 결과가 없습니다.", Toast.LENGTH_LONG);
}
} catch (MalformedURLException e) {
Toast.makeText(this, "오류1", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "오류2", Toast.LENGTH_SHORT).show();
}
}
}



