Я бы хотел реализовать серверный вызов NanoHttpd в моем «MainActivity». В моей MainActivity я получаю два значения ввода текста и вызываю другое действие ValidCodeReceived и передаю эти значения. Как я могу вызвать NanoHttpd
в моем MainActivity и получить эти значения? Пожалуйста, сообщите
// Код сервера 'NanoHttpd':
public class MyServer extends NanoHTTPD {
private final static int PORT = 8080;
public MyServer() throws IOException {
super(PORT);
start();
}
@Override
public Response serve(IHTTPSession session) {
return new NanoHTTPD.Respon( );
}
}
@Override
public void onResume() {
super.onResume();
try {
server = new MyServer();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onPause() {
super.onPause();
if(server != null) {
server.stop();
}
}
// Существующий код 'MainActivity' ниже:
public class MainActivity extends AppCompatActivity {
private EditText inputName, inputCode;
public static String strName = null;
public static String strCode = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputName = (EditText) findViewById(R.id.editName);
inputName.setTextColor(Color.parseColor("#63d0f2"));
inputCode = (EditText) findViewById(R.id.editCode);
Button button = findViewById(R.id.displayMap);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
strName = inputName.getText().toString();
strCode = inputCode.getText().toString();
String myCode = inputCode.getText().toString();
if(inputName.getText().length()<3 || inputName.getText() ==null ){
Toast.makeText(MainActivity.this, "Please enter valid name !",Toast.LENGTH_LONG).show();
}else if (inputCode.getText().length()<4){
Toast.makeText(MainActivity.this, "Please enter a valid code !",Toast.LENGTH_LONG).show();
}else if(myCode.matches("")){
Toast.makeText(MainActivity.this, "Code cannot be blank !",Toast.LENGTH_LONG).show();
}else if(!myCode.matches(".*[A-Za-z].*") && !myCode.matches(".*[0-9].*") && !myCode.matches("[A-Za-z0-9]*")){
Toast.makeText(MainActivity.this, "Code field should have alpha numeric values",Toast.LENGTH_LONG).show();
}
else {
Intent intent = new Intent(MainActivity.this, ValidCodeReceived.class);
intent.putExtra("mainname", strName);
intent.putExtra("maincode", strCode);
startActivity(intent);
}
}
});
}
@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);
}
}
// ValidCodeReceived
public class ValidCodeReceived extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( getIntent().getExtras() != null)
{
Intent intent = getIntent();
String myData1 = intent.getStringExtra("mainname");
String myData2 = intent.getStringExtra("maincode");
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText("Hello team, send by:"+ myData1 + " "+ myData2);
setContentView(textView);
}
}
}