2014-06-14 4 views
0

그래서 이것을 테스트하기 위해 here에서 소스 코드를 얻었고 서버에서 출력 또는 응답을 얻지 못했습니다. 왜 이런 일이 일어나는 지 아는 사람이 있습니까? 나는 두 번째 "시도"에서 무언가가 작동하지 않고 그 안에 아무것도없는 exclusion ex에 들어가는 느낌을 가지고있다. 이 올바른지? 어쨌든 저에게 응답/결과를주기 위해 저를 도와주십시오.메신저와 같은 출력을 얻지 못했습니다

public class HttpPostExample extends Activity { 

    TextView content; 
    EditText fname, email, login, pass; 
    String Name, Email, Login, Pass; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_http_post_example); 

     content = (TextView)findViewById(R.id.content); 
     fname  = (EditText)findViewById(R.id.name); 
     email  = (EditText)findViewById(R.id.email); 
     login  = (EditText)findViewById(R.id.loginname); 
     pass  = (EditText)findViewById(R.id.password); 


     Button saveme=(Button)findViewById(R.id.save); 

     saveme.setOnClickListener(new Button.OnClickListener(){ 

      public void onClick(View v) 
      { 
       try{ 

         // CALL GetText method to make post method call 
         GetText(); 
       } 
       catch(Exception ex) 
       { 
        content.setText(" url exeption! "); 
       } 
      } 
     }); 
    }  
// Create GetText Metod 
public void GetText() throws UnsupportedEncodingException 
    { 
     // Get user defined values 
     Name = fname.getText().toString(); 
     Email = email.getText().toString(); 
     Login = login.getText().toString(); 
     Pass = pass.getText().toString(); 

     // Create data variable for sent values to server 

     String data = URLEncoder.encode("name", "UTF-8") 
        + "=" + URLEncoder.encode(Name, "UTF-8"); 

     data += "&" + URLEncoder.encode("email", "UTF-8") + "=" 
        + URLEncoder.encode(Email, "UTF-8"); 

     data += "&" + URLEncoder.encode("user", "UTF-8") 
        + "=" + URLEncoder.encode(Login, "UTF-8"); 

     data += "&" + URLEncoder.encode("pass", "UTF-8") 
        + "=" + URLEncoder.encode(Pass, "UTF-8"); 

     String text = ""; 
     BufferedReader reader=null; 

     // Send data 
     try 
     { 

      // Defined URL where to send data 
      URL url = new URL("http://androidexample.com/media/webservice/httppost.php"); 

     // Send POST data request 

     URLConnection conn = url.openConnection(); 
     conn.setDoOutput(true); 
     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
     wr.write(data); 
     wr.flush(); 

     // Get the server response 

     reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     // Read Server Response 
     while((line = reader.readLine()) != null) 
      { 
       // Append server response in string 
       sb.append(line + "\n"); 
      } 


      text = sb.toString(); 
     } 
     catch(Exception ex) 
     { 

     } 
     finally 
     { 
      try 
      { 

       reader.close(); 
      } 

      catch(Exception ex) {} 
     } 

     // Show response on activity 
     content.setText(text ); 

    } 

}

편집 : 난 그냥, 사람이 어디에 정확히 알고 디버깅을했고, 내가 문제가 여기에서의 어딘가에 thwere에 대한 권리 것을 발견?

 URL url = new URL("http://androidexample.com/media/webservice/httppost.php"); 

    // Send POST data request 

     URLConnection conn = url.openConnection(); 
     conn.setDoOutput(true); 
     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
     wr.write(data); 
     wr.flush(); 

     // Get the server response 

    reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 

    // Read Server Response 
    while((line = reader.readLine()) != null) 
     { 
       // Append server response in string 
       sb.append(line + "\n"); 
     } 


     text = sb.toString(); 

    } 
+0

왜 뭐가 있는지보고 단계별 디버깅을 사용하지 계속 하시겠습니까? – ben75

+0

안드로이드 매니페스트에''을 추가 했습니까? – PKlumpp

+0

네, 제가 추가했습니다. – user3404539

답변

0

메인 (UI) 스레드에서 네트워크 입출력을하고 있습니다. 즉, not allowed in Android입니다.

모든 코드를 AsyncTask으로 옮겨야합니다. 그러나 백그라운드 스레드에서 UI를 변경할 수 없으므로 결과를 표시하는 부분은 onPostExecute()에서 실행해야합니다.

예를 들어, 이런 식으로 클릭 리스너를 대신 content.setText()를 호출하는 텍스트와 문자열을 반환하기 위해 GetText() 방법을 변경 :

public void onClick(View v) 
{ 
    // Get user defined values 
    Name = fname.getText().toString(); 
    Email = email.getText().toString(); 
    Login = login.getText().toString(); 
    Pass = pass.getText().toString(); 

    new AsyncTask<Void, Void, String>() 
    { 
     @Override 
     public String doInBackground (Void... params) 
     { 
      return GetText(); 
     } 

     @Override 
     protected void onPostExecute(String result) 
     { 
      content.setText(result); 
     } 

    }.execute(); 
} 
+0

괜찮아 병이 감사 :)의 getView에 의해 – user3404539

+0

() 메소드를이 밖으로 시도 당신은 의미합니까 GetText()? – user3404539

+0

예, getText(). 죄송합니다. – matiash

관련 문제