2016-07-12 7 views
0

私のアンドロイドアプリケーションからサーバーにデータを送信しようとしています。私が使っているサーバーはEasyPHPです。私のonCreate()関数の中で、私が持っている:.getOutputStream()がアプリケーションをクラッシュさせ続ける

user_editbox = (EditText) findViewById(R.id.username_id); 
    phone_editbox = (EditText) findViewById(R.id.phonenum_id); 
    submit_button = (Button) findViewById(R.id.submit_id); 



    submit_button.setOnClickListener(new View.OnClickListener() { 

       String username = "Gina"; 
       String phone_number = "2225550000"; 
        public void onClick(View view) 
        { 


         Log.d(TAG, username); 
         Log.d(TAG, phone_number); 
         if(username != null && phone_number != null) 
         { 
          Log.d(TAG, "Inside " + username); 
          Log.d(TAG, "Inside " + phone_number); 

          try { 
           Log.d(TAG, "Entered try"); 
           URL url = new URL("http://localhost/Web%20/Practice/Android%20Login/phpserver.php"); 
           Log.d(TAG, "URL given"); 
           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
           urlConnection.setRequestMethod("POST"); 
           urlConnection.setDoOutput(true); 




           Uri.Builder builder = new Uri.Builder() 
             .appendQueryParameter("Username ", username) 
             .appendQueryParameter("PhoneNumber ", phone_number); 
           String query = builder.build().getEncodedQuery(); 


           OutputStream os = urlConnection.getOutputStream(); // <--- Problem 
           BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
           writer.write(query); 
           writer.flush(); 
           writer.close(); 
           os.close(); 
           urlConnection.connect(); 

          }catch(IOException e) 
          { 
           e.printStackTrace(); 
          } 

プログラムが到達したときに問題が発生し

 OutputStream os = urlConnection.getOutputStream(); 

ダイアログボックスが

Unfortunately, Program has stopped 

私は何を理解しようとしているのを知らせる表示されます強調表示されたコードでプログラムがクラッシュする可能性があります。サーバはスムーズに動作しているようで、PHPスクリプトは次のようになります。

アイデア?

Logcat

07-11 22:17:30.118 2645-2645/com.example.czar.connectingtoserver D/MSG: onCreate 
    07-11 22:17:30.120 2645-2645/com.example.czar.connectingtoserver D/MSG: onStart 
    07-11 22:17:30.122 2645-2645/com.example.czar.connectingtoserver D/MSG: onResume 
    07-11 22:19:24.758 2645-2645/com.example.czar.connectingtoserver D/MSG: Clicked 
    07-11 22:19:24.758 2645-2645/com.example.czar.connectingtoserver D/MSG: Gina 
    07-11 22:19:24.758 2645-2645/com.example.czar.connectingtoserver D/MSG: 2225550000 
    07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Inside Gina 
    07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Inside 2225550000 
    07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Entered try 
    07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: URL given 
    07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Output set to true 
    07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: String Encoded 
+0

あなたはlogcatを編集して追加できますか? – DAVIDBALAS1

答えて

0

クラッシュNetworkOnMainThreadExceptionがここにありますので。 HttpURLConnectionとのすべての作業をThreadにする必要があります。

+0

私はすべてのプログラムが通常少なくとも1つのスレッドを実行していると思った? – Czar

+0

あなたのケースでは、onClickイベントのブロックをメインスレッドで実行できません。別のスレッドに分割する必要があります。 – phongvan

関連する問題