package com.capital.connectmysql; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private UploadTask task; private TextView textView; private EditText editText; String url = "http://hpscript/android/pass_check.html"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.uriname); Button post = findViewById(R.id.post); post.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ String param0 = editText.getText().toString(); if(param0.length() != 0){ task = new UploadTask(); task.setListener(createListener()); task.execute(param0); } } }); Button browser = findViewById(R.id.browser); browser.setOnClickListener(new View.OnClickListenr(){ @Override public void onClick(View v){ Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); textView.setText(""); } }); textView = findViewById(R.id.text_view); } @Override protected void onDestroy(){ task.setListener(null); super.onDestory(); } private UploadTask.Listener createListener(){ return new UploadTask.Listener(){ @Override public void onSuccess(String result){ textView.setText(result); } }; } }
package com.capital.connectmysql; import android.net.sip.SipAudioCall; import android.os.AsyncTask; import android.util.Log; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class UploadTask extends AsyncTask<String, Void, String> { private SipAudioCall.Listener listener; @Override protected String doInBackground(String... params){ String urlSt = "http://hpscript.com/"; HttpURLConnection con = null; String result = null; String word = "word="+params[0]; try { URL url = new URL(urlSt); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setInstanceFollowRedirects(false); con.setDoOutput(true); con.setReadTimeout(10000); con.setConnectTimeout(20000); con.connect(); OutputStream out = null; try { out = con.getOutputStream(); out.write( word.getBytes("UTF-8")); out.flush(); Log.d("dubug", "flush"); } catch (IOException e){ e.printStackTrack(); result = "POST送信エラー"; } finally { if (out != null){ out.close(); } } final int status = con.getResponseCode(); if (status == HttpURLConnection.HTTP_OK){ result = "HTTP_OK"; } else { result = "status="+String.valueOf(status); } } catch(IOException e){ e.printStackTrace(); } finally { if (con != null){ con.disconnect(); } } return result; } @Override protected void onPostExecute(String result){ super.onPostExecute(result); if(listener != null){ listener.onSuccess(result); } } void setListener(SipAudioCall.Listener listener){ this.listener = listener; } interface Listener { void onSuccess(String result); } }