Button.OnClickListener buttonClick = new Button.OnClickListener() {

  public void onClick(View v) {

   // 사용자가 입력한 내용을 전역변수에 저장한다

   myId = ((EditText) (findViewById(R.id.edit_Id))).getText()
     .toString();

    myPWord = ((EditText) (findViewById(R.id.edit_pword))).getText()
    .toString();

    myTitle = ((EditText) (findViewById(R.id.edit_title))).getText()
    .toString();

    mySubject = ((EditText) (findViewById(R.id.edit_subject)))
    .getText().toString();

   HttpPostData(); // 서버와 자료 주고받기

  }

 };

 //
 // ------------------------------

 // Http Post로 주고 받기

 // ------------------------------

 public void HttpPostData() {

  try {

   // --------------------------

   // URL 설정하고 접속하기

   // --------------------------

   URL url = new URL("http://서버 ip /test.php");


   // URL 설정

   HttpURLConnection http = (HttpURLConnection) url.openConnection(); // 접속

   // --------------------------

   // 전송 모드 설정 - 기본적인 설정이다

   // --------------------------

   http.setDefaultUseCaches(false);

   http.setDoInput(true); // 서버에서 읽기 모드 지정

   http.setDoOutput(true); // 서버로 쓰기 모드 지정

   http.setRequestMethod("POST"); // 전송 방식은 POST

   // 서버에게 웹에서 <Form>으로 값이 넘어온 것과 같은 방식으로 처리하라는 걸 알려준다

   http.setRequestProperty("content-type",
     "application/x-www-form-urlencoded");

   // --------------------------

   // 서버로 값 전송

   // --------------------------

   StringBuffer buffer = new StringBuffer();

   buffer.append("id").append("=").append(myId).append("&");

   // php 변수에 값 대입

   buffer.append("pword").append("=").append(myPWord).append("&"); // php
                   // 변수
                   // 앞에
                   // '$'
                   // 붙이지
                   // 않는다

   buffer.append("title").append("=").append(myTitle).append("&"); // 변수
                   // 구분은
                   // '&'
                   // 사용

   buffer.append("subject").append("=").append(mySubject);

   OutputStreamWriter outStream = new OutputStreamWriter(
     http.getOutputStream(), "EUC-KR");

   Log.d("check", "1" + myId);

   PrintWriter writer = new PrintWriter(outStream);

   Log.d("check", "2");

   writer.write(buffer.toString());

   writer.flush();

   Log.d("check", "3");

   // --------------------------

   // 서버에서 전송받기

   // --------------------------

   InputStreamReader tmp = new InputStreamReader(
     http.getInputStream(), "EUC-KR");

   BufferedReader reader = new BufferedReader(tmp);

   StringBuilder builder = new StringBuilder();

   String str;

   while ((str = reader.readLine()) != null) { // 서버에서 라인단위로 보내줄 것이므로
              // 라인단위로 읽는다

    builder.append(str + "\n"); // View에 표시하기 위해 라인 구분자 추가

   }

   myResult = builder.toString(); // 전송결과를 전역 변수에 저장

   Log.d("check", "Stirng" + myResult);

   ((TextView) (findViewById(R.id.text_result))).setText(myResult);

   Toast.makeText(MainActivity.this, "전송 후 결과 받음", 0).show();

  } catch (MalformedURLException e) {

   //

  } catch (IOException e) {

   //

  } // try

 } // HttpPostData

} // Activity

 

 

 

php

 

<?php

$adress="localhost";
$link = mysql_connect($adress, 'root', 'root');
mysql_query("set names utf8");
$phone=$_REQUEST[telephone];

// android ->  php -> mysql db에 저장될 변수

if (!$link) {
 die('Could not connect: ' . mysql_error());
}else{
 echo '<center>Connected successfully!!!!!';
}
////////////////////////////////////////mysql 연결 / 오류처리

$db_selected = mysql_select_db('test', $link);
if (!$db_selected) {
 die ('Can\'t use foo : ' . mysql_error());
}else{
 echo
 '<center>db use successfully';
}
///////////////////////////////////////DB 선택 / 오류처리

$query="INSERT INTO User_Info VALUES('1111') ";
//$query="INSERT INTO User_Info VALUES('$phone') ";

$result=mysql_query($query);

if (!$result) {
 die('Invalid query: ' . mysql_error());
}
$xml = "<?xml version = \"1.0\" encoding = \"utf-8\"?-->\n";
$xml .= "<result>$result</result>\n";
$dir = "C:/APM_Setup/htdocs";
$filename = $dir."/phone.xml";
file_put_contents($filename, $xml);


//$rows=mysql_num_rows($result);


////////////////////////////////////mysql 쿼리 처리


/*
 $rows=mysql_num_rows($result);
echo "<center><font size=2>$rows 개의 행이 검색되었습니다.</font>";
$i = 0;

while ($i<$rows) {
$row = mysql_fetch_row($result);
echo "아이디: ".$row[0]."    ";
echo "패스워드: ".$row[1]."    ";

echo "<br>";
$i++;
}

*/
//////////////////////////////////쿼리 전송

mysql_close($link);
?>

 

 

소스는 이러 합니다.

 

EdiText 입력 후 버튼을 누르면  Http Post 방식으로  android -> php에 전송해주고 다시 php->android로 출력해주는 예제 인데요.

 

웹브라우저, 다른 pc웹브라우저, 어큐뮬레이터로는 정상적으로  동작 합니다.

 

근데 핸드폰으로 실행해서 버튼 누르면, "예상치 않게 중지되었습니다." 라고 오류가 떠서 어플이 죽어요.

 

왜 그럴까요?