お世話になっております。
androidからpostしても、TimeoutErrorを吐かれてしまいどう修正すればよいのか困っております。
以下が処理部分の抜粋になります。
ご教授よろしくお願いいたします。
java
1 2String url = "http://localhost/sample.php"; 3 4.... 5 6private void transport(final String name) { 7 8 final Map<String,String> params = new HashMap<>(); 9 params.put("name",name); 10 11 JsonObjectRequest request = 12 new JsonObjectRequest( 13 Method.POST, url,params,new Response.Listener<JSONObject>() { 14 @Override 15 public void onResponse(JSONObject response) { 16 Log.d(TAG, response.toString()); 17 try { 18 19 String name = (response.getString("name")); 20 String colleague = (response.getString("colleague")); 21 String result = colleague +","+ name ; 22 showDialogMessage(result, true); 23 24 } catch (JSONException e) { 25 e.printStackTrace(); 26 } 27 } 28 }, new Response.ErrorListener() { 29 @Override 30 public void onErrorResponse(VolleyError error) { 31 Log.d(TAG,error.toString());//ここでタイムアウトエラー 32 } 33 }); 34 35 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 36 requestQueue.add(request); 37 38 } 39
php
1 2<?php 3 4function h($str) { 5 return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 6} 7 $array = null; 8 $body = file_get_contents('php://input'); 9 $postvars = json_decode($body, true); 10 $name = $postvars['name']; 11 12try { 13 $pdo = new PDO( 14 'mysql:dbname=test;host=localhost;charset=utf8', 15 'test', 16 '', 17 array( 18 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 19 PDO::ATTR_EMULATE_PREPARES => false, 20 ) 21 ); 22 23 $stmt = $pdo->prepare("SELECT name,colleague FROM test.table where name =:name"); 24 $stmt ->bindValue(':name',$name); 25 $stmt ->execute(); 26 27 while ($row = $stmt -> fetch()) { 28 29 $array = array( 30 "name" => $row['name'], 31 "colleague" => $row['colleague'] 32 ); 33 } 34 35header('Content-Type: application/json; charset=utf-8'); 36$json = json_encode($array,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE); 37echo $json 38 39} catch (PDOException $e) { 40 $error = $e->getMessage(); 41} 42 43?> 44 45 46
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。