2017-10-03 1 views
0

저는 C++ 애호가입니다. 그러나 대학 과제를 수행하려면 Android (Java), Web (PHP) 및 Arduino를 코딩해야하며 경험이 없습니다. 자바.HTTP POST를 통해 Android, PHP 및 Arduino 간의 통신을 시도하고 있습니다.

나는 적어도 안드로이드 코드를 시도했지만, 컴파일되지는 않을 것이고, 나는 이유를 알 수 없다고 말한다. "클래스, 인터페이스 또는 열거 형 예상 오류"가 많아서 어디서나 찾을 수 없습니다.

(한국어를 무시하고 한국 대학에 가면 교수님이 포함 시키길 원합니다. 내 프로그램에서 한국어).

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;입니다해야 하는가?