2017-10-03 2 views
0

私はC++の恋人ですが、私の大学の割り当てにはAndroid(Java)、Web(PHP)、Arduinoをコードする必要があります。 Java。私はAndroid、PHPとArduinoの間でHTTP POSTを使って通信しようとしています

私は少なくともAndroidのコードを試しましたが、コンパイルされないと言われています。理由は分かりません。

(ああ、私は韓国の大学に行って、私は韓国の大学に行きたいと思っています。私のプログラムでは韓国語)。

package ---my package---; 

import android.os.Build; 
import android.os.Bundle; 
import android.support.annotation.RequiresApi; 
import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.net.ssl.HttpsURLConnection; 

import static android.graphics.Color.GREEN; 
import static android.graphics.Color.RED; 

public class MainActivity extends AppCompatActivity { 
    Button mp3_1, mp3_2, mp3_3, mp3_4, finishbutton; 
    TextView wakeornot; 
    char datatosend; 
    String datareceived; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     wakeornot = (TextView) findViewById(R.id.wakeornot); 
     mp3_1 = (Button) findViewById(R.id.mp3_1); 
     mp3_2 = (Button) findViewById(R.id.mp3_2); 
     mp3_3 = (Button) findViewById(R.id.mp3_3); 
     mp3_4 = (Button) findViewById(R.id.mp3_4); 
     finishbutton = (Button) findViewById(R.id.finishbutton); 

     mp3_1.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.KITKAT) 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "1번 자장가를 재생합니다", Toast.LENGTH_SHORT).show(); 
       datatosend = '1'; 

       HttpPostData(); 
      }}); 

     mp3_2.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.KITKAT) 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "2번 자장가를 재생합니다", Toast.LENGTH_SHORT).show(); 
       datatosend = '2'; 

       HttpPostData(); 
      }}); 

     mp3_3.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.KITKAT) 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "3번 자장가를 재생합니다", Toast.LENGTH_SHORT).show(); 
       datatosend = '3'; 

       HttpPostData(); 
      }}); 

     mp3_4.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.KITKAT) 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "4번 자장가를 재생합니다", Toast.LENGTH_SHORT).show(); 
       datatosend = '4'; 

       HttpPostData(); 
      }}); 

     finishbutton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "프로그램을 종료합니다", Toast.LENGTH_SHORT).show(); 
       finish(); 
      }}); 
    } 

    @RequiresApi(api = Build.VERSION_CODES.KITKAT) 
    public void HttpPostData() { 
     try { 
      //URL 설정 및 접속 
      URL url = new URL("https://---domain---.net"); 
      HttpsURLConnection http = (HttpsURLConnection) url.openConnection(); 

      //전송 모드 설정 
      http.setDefaultUseCaches(false); 
      http.setDoInput(true);       // 서버에서 읽기 모드 지정 
      http.setDoOutput(true);      // 서버로 쓰기 모드 지정 
      http.setRequestMethod("POST");     // 전송 방식은 POST 

      http.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 
      //서버로 값 전송 
      StringBuffer buffer; 
      buffer = new StringBuffer(); 
      buffer.append("datatosend").append("=").append(datatosend); 

      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).append("\n");      // View에 표시하기 위해 라인 구분자 추가 
      } 
      datareceived = builder.toString();     // 전송결과를 전역 변수에 저장 

      if (datareceived.equals("5")) { 
       datareceived = "! 깨어 !"; 
       ((TextView)(findViewById(R.id.wakeornot))).setText(datareceived); 
       ((TextView)(findViewById(R.id.wakeornot))).setTextColor(RED); 
       Toast.makeText(MainActivity.this, "아기의 상태에 변화가 있습니다", Toast.LENGTH_SHORT).show(); 
      } 

      else if (datareceived.equals("6")) { 
       datareceived = "자고"; 
       ((TextView)(findViewById(R.id.wakeornot))).setText(datareceived); 
       ((TextView)(findViewById(R.id.wakeornot))).setTextColor(GREEN); 
       Toast.makeText(MainActivity.this, "아기의 상태에 변화가 있습니다", Toast.LENGTH_SHORT).show(); 
      } 

     } catch (IOException e) { 
      // 
     } // try 
    } 
} 

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

答えて

0

私はエラーを「期待クラス、インターフェイス、または列挙型」の束を取得し、私は単にどこでも

あなたは以下の前に余分なブラケットを持ってそれを上に見つけることができません コード。それを除く。

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.menu_main, menu); 
return true; 
} 
  • あなたのパッケージ名がpackage my package;であるべきではないのですか?

  • 関連する問題