解決したいことはandroidからpost送信しcakephpで受け取ることです。
androidのEditText で入力した内容がcakephpで受け取れていないようです。
cakephpのindexページへの遷移はできていますがpost送信できておらず、
cakephpのcontrollerの受け取り方に問題があるのでしょうか?
android-Logcat
09-23 00:42:37.513 7972-7993/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 09-23 00:42:37.632 7972-7993/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da 09-23 00:42:37.633 7972-7993/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da 09-23 00:42:38.566 7972-7972/com.example.jyun.myapplication W/View: dispatchProvideAutofillStructure(): not laid out, ignoring
上記のエラーに対応するため仮想デバイスのエミュレートパフォーマンス→グラフィックスをSoftwareに変更していますがうまくいきませんでした。
android-MainActivity
java
1public class MainActivity extends AppCompatActivity { 2 3 private UploadTask task; 4 private TextView textView; 5 private EditText editText; 6 7 String url = "http://cake/books/index"; 8 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 editText = findViewById(R.id.uriname); 16 17 Button post = findViewById(R.id.post); 18 19 post.setOnClickListener(new View.OnClickListener() { 20 @Override 21 public void onClick(View v) { 22 String param0 = editText.getText().toString(); 23 24 if(param0.length() != 0){ 25 task = new UploadTask(); 26 task.setListener(createListener()); 27 task.execute(param0); 28 } 29 30 } 31 }); 32 33 Button browser = findViewById(R.id.browser); 34 browser.setOnClickListener(new View.OnClickListener() { 35 @Override 36 public void onClick(View v) { 37 Uri uri = Uri.parse(url); 38 Intent intent = new Intent(Intent.ACTION_VIEW,uri); 39 startActivity(intent); 40 41 textView.setText(""); 42 } 43 }); 44 45 textView = findViewById(R.id.text_view); 46 } 47 48 49 @Override 50 protected void onDestroy() { 51 task.setListener(null); 52 super.onDestroy(); 53 } 54 55 private UploadTask.Listener createListener() { 56 return new UploadTask.Listener() { 57 @Override 58 public void onSuccess(String result) { 59 textView.setText(result); 60 } 61 }; 62 } 63}
android-UploadTask
java
1public class UploadTask extends AsyncTask<String, Void, String> { 2 3 private Listener listener; 4 5 @Override 6 protected String doInBackground(String... params) { 7 8 String urlSt = "http://cake/books/index"; 9 10 HttpURLConnection con = null; 11 String result = null; 12 String word = "word="+params[0]; 13 14 try { 15 URL url = new URL(urlSt); 16 17 con = (HttpURLConnection) url.openConnection(); 18 19 con.setRequestMethod("POST"); 20 21 con.setInstanceFollowRedirects(false); 22 23 con.setDoOutput(true); 24 25 con.setReadTimeout(10000); 26 con.setConnectTimeout(20000); 27 28 con.connect(); 29 30 OutputStream out = null; 31 try { 32 out = con.getOutputStream(); 33 out.write( word.getBytes("UTF-8") ); 34 out.flush(); 35 Log.d("debug","flush"); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 result="POST送信エラー"; 39 } finally { 40 if (out != null) { 41 out.close(); 42 } 43 } 44 45 final int status = con.getResponseCode(); 46 if (status == HttpURLConnection.HTTP_OK) { 47 result="HTTP_OK"; 48 } 49 else{ 50 result="status="+String.valueOf(status); 51 } 52 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } finally { 56 if (con != null) { 57 con.disconnect(); 58 } 59 } 60 return result; 61 } 62 63 @Override 64 protected void onPostExecute(String result) { 65 super.onPostExecute(result); 66 67 if (listener != null) { 68 listener.onSuccess(result); 69 } 70 } 71 72 void setListener(Listener listener) { 73 this.listener = listener; 74 } 75 76 interface Listener { 77 void onSuccess(String result); 78 } 79}
cakephp-controller
php
1class BooksController extends AppController 2{ 3 public function index() 4 { 5 $add = ""; 6 if( isset($this->request->data['id'])){ 7 $id = $this->request->data['id']; 8 $this->set( 'num' , $id); 9 }else{ 10 $this->Flash->error('postエラー', 11 'default', array('class'=>'message_error')); 12 } 13 } 14}
cakephp-index
ctp
1<div> 2<?php 3 if( isset($num) ) 4 { 5 pr($num); 6 } 7?> 8</div>
利用環境
android studio3.1.3
cakephp3
参考にしているサイト
https://akira-watson.com/android/httpurlconnection-post.html
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/06 03:47