마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

PHP서버 DB교환 post 방식 오류

0 추천

안녕하세요 php공부중인데요 이 예제를 실행해 봤더니 어플에서 정보 입력하고 서버로 전송 버튼을 누르면 오류가 나면서 어플이 꺼지네요 왜 그런지 모르겠습니다 도와주세요 ㅜㅜㅜ

8000자 밖에 못 써서 전체 소스와 실행화면 사진은

http://www.androidside.com/bbs/board.php?bo_table=421&wr_id=137 

여기에 있습니다

나는안드로오 (960 포인트) 님이 2014년 8월 7일 질문

2개의 답변

+1 추천
 
채택된 답변
기본적으로 서버 주소는 바꿔주셨죠?
ysy00115 (2,350 포인트) 님이 2014년 8월 7일 답변
나는안드로오님이 2014년 8월 7일 채택됨
서버주소 바꿔 봣어요 근데 저 예제에서는 서버 그대로 있길래 바꾸고 해보고 제껄로도 해봤습니다!
그럼 로그캣을 확인해보시고 무슨 에러가 났는지 OR 예외가 났는지 확인해보세요
로그캣 확인하구
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
이거 추가해주고
manifest에서서
 android:minSdkVersion="9"로 높여주니깐
잘됩니다! ㅎㅎ
가장 기본적으로 로그캣부터 확인해봤어야 하는데 제가 멘탈이 약해 졌나보네요...ㅎㅎ감사합니다!!!
0 추천
링크를 볼 수가 없네요.
진짱 (720 포인트) 님이 2014년 8월 7일 답변
<?xml version="1.0" encoding="utf-8"?>
<TabHost
     xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/tabhost"  
      android:layout_width="fill_parent"  
      android:layout_height="fill_parent">

      <LinearLayout  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:orientation="vertical">  

           <TabWidget  
                 android:id="@android:id/tabs"  
                 android:layout_width="fill_parent"  
                 android:layout_height="wrap_content"/>

           <TextView
                 android:layout_width="wrap_content"  
                 android:layout_height="wrap_content"/>
           <FrameLayout  
                 android:id="@android:id/tabcontent"  
                 android:layout_width="fill_parent"  
                 android:layout_height="fill_parent">

            <TableLayout  
                  android:id="@+id/page01"  
                  android:layout_width="fill_parent"  
                  android:layout_height="wrap_content"  
                  android:stretchColumns="1">

             <TableRow>
                   <TextView  
                         android:layout_width="wrap_content"  
                         android:layout_height="wrap_content"
                         android:text="ID :"/>
                   <EditText  
                          android:id="@+id/edit_Id"  
                          android:layout_width="fill_parent"  
                          android:layout_height="wrap_content"/>
             </TableRow>
             <TableRow>
                   <TextView  
                         android:layout_width="wrap_content"  
                         android:layout_height="wrap_content"  
                         android:text="PWord : "/>
                    <EditText  
                          android:id="@+id/edit_pword"  
                          android:layout_width="fill_parent"  
                          android:layout_height="wrap_content"  
                          android:password="true"/>
             </TableRow>
             <TableRow>
                   <TextView  
                         android:layout_width="wrap_content"  
                         android:layout_height="wrap_content"  
                         android:text="개념 : "/>
                    <EditText  
                          android:id="@+id/edit_title"  
                          android:layout_width="fill_parent"  
                          android:layout_height="wrap_content"/>
             </TableRow>
             <TableRow>
                   <TextView  
                         android:layout_width="wrap_content"  
                         android:layout_height="wrap_content"  
                         android:text="대책 : "/>
                    <EditText  
                          android:id="@+id/edit_subject"  
                          android:layout_width="fill_parent"  
                          android:layout_height="wrap_content"  
                          android:lines="4"/>
             </TableRow>
             <View  
                   android:layout_height="2dip"  
                   android:background="#AAAAAA"/>
             <TableRow>
                   <Button  
                         android:text="   전    송    "  
                         android:id="@+id/button_submit"  
                         android:layout_column="1"
                         android:layout_width="wrap_content"  
                         android:layout_height="wrap_content"/>
             </TableRow>
       </TableLayout>

  <LinearLayout  
        android:id="@+id/page02"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content">
        <TextView  
              android:id="@+id/text_result"  
              android:layout_width="fill_parent"  
              android:layout_height="wrap_content"/>
       </LinearLayout>
   </FrameLayout>   
</LinearLayout>
</TabHost>
package com.http_post;

import java.io.*;
import java.net.*;

import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends TabActivity {
    // 전역변수를 선언한다
    TabHost mTabHost = null;
    String myId, myPWord, myTitle, mySubject, myResult;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTabHost = getTabHost();          // Tab 만들기
        mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator("서버로 전송").setContent(R.id.page01));
        mTabHost.addTab(mTabHost.newTabSpec("tab_2").setIndicator("서버에서 받음").setContent(R.id.page02));
        findViewById(R.id.button_submit).setOnClickListener(buttonClick);  
    }
    
    //------------------------------
    //    button Click
    //------------------------------
    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://korea-com.org/foxmann/lesson01.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");
              PrintWriter writer = new PrintWriter(outStream);
              writer.write(buffer.toString());
              writer.flush();
              //--------------------------
              //   서버에서 전송받기
              //--------------------------
              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();                       // 전송결과를 전역 변수에 저장
             ((TextView)(findViewById(R.id.text_result))).setText(myResult);
             Toast.makeText(MainActivity.this, "전송 후 결과 받음", 0).show();
         } catch (MalformedURLException e) {
                //
         } catch (IOException e) {
                //  
         } // try
    } // HttpPostData
} // Activity

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡphp
<?
// 변수 내용 확인
if ($id == "") $id = "너는 ID도 없냐?";
if ($pword == "") $pword = "칠칠맞게 비밀번호도 잊어먹고 다니네...";
if ($title == "") $title = "증말 개념없는 사람일세...";
if ($subject == "") $subject = "에구~~ 대책이 없네...";
// 변수 내용 출력
echo ("
  님께서 PHP로 전송한 내용입니다
  -----------------------------------------------------------------\r\n
  사용자 ID : $id \r\n
  비밀번호 : $pword \r\n
  개념 : $title \r\n
  대책 : $subject \r\n
  -----------------------------------------------------------------\r\n
  축하드립니다. lesson01.php 를 정삭적으로 호출하셨습니다!
");
?>
댓글은 소스가 올라가네요
...