急にUnityとサーバーにあるphpと通信が出来なくなりました。
一度通信が出来ていたために、何が問題かわかりません。
やったこと
phpファイルはxamppのサーバーに配置しているので、apacheのエラーログやアクセスログを確認してみました。
結果エラーログには何も出ておらず、アクセスログもありませんでした。
またUnityからデータを入力するのにはドロップダウンボタンやボタンを使っているのですが
そちらにスクリプトが張られているかどうかも確認しました。しかし
スクリプトも問題なく指定できていました。
下に問題のコードを張ります。
www.errorには
HttpPost NG:Request timeout
と帰ってきました。
追記
まさか関係ないとは思うのですが
フォントをデフォルトのArialから別のフォントに変えました。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.Networking; 6public class PHPTEST : MonoBehaviour 7{ 8 public Text ResultText_; 9 public GameObject Drop_1; 10 public GameObject Drop_2; 11 Dropdown_1 drop; 12 Dropdown_2 drop2; 13 private string time; 14 public int callory_time; 15 private string ServerAddress = "http://localhost/php/API_req.php"; 16 // Use this for initialization 17 void Start() 18 { 19 drop = Drop_1.GetComponent<Dropdown_1>(); 20 drop2 = Drop_2.GetComponent<Dropdown_2>(); 21 } 22 public void SendSignal_Button_Push() 23 { 24 //ドロップダウンボタンでNavitimeみたいに駅名を指定し、駅名が入力されていなければ 25 //デバッグログで返す、というこれも動かないです 26 if(drop.rel==null||drop2.rel2==null){ 27 Debug.Log("値が入力されていません"); 28 ResultText_.GetComponent<Text>().text = null; 29 return; 30 } 31 StartCoroutine("Access"); 32 Debug.Log(drop.rel); 33 Debug.Log(drop2.rel2); 34 } 35 36 private IEnumerator Access() 37 { 38 //ドロップダウンで指定した駅をDictionaryで連想配列をつくる 39 Dictionary<string, string> dic = new Dictionary<string, string> 40 { 41 { "id_1", drop.rel}, 42 { "id_2", drop2.rel2 } 43 }; 44 Debug.Log(dic["id_1"]); 45 Debug.Log(dic["id_2"]); 46 StartCoroutine(Post(ServerAddress, dic)); 47 yield return 0; 48 } 49 50 private IEnumerator Post(string url, Dictionary<string, string> post) 51 { 52 //WWWformにドロップダウンから入力されたデータを入れる 53 WWWForm form = new WWWForm(); 54 foreach (KeyValuePair<string, string> post_arg in post) 55 { 56 form.AddField(post_arg.Key, post_arg.Value); 57 } 58 //WWWformをUnityWebRequestでPOSTする 59 UnityWebRequest www = UnityWebRequest.Post(ServerAddress,form); 60 61 Debug.Log(ServerAddress); 62 www.timeout = 30; 63 yield return www.SendWebRequest(); 64 65 if (www.error != null) 66 { 67 Debug.Log("HttpPost NG:" + www.error); 68 } 69 else if (www.isDone) 70 { 71 //返ってきたデータを表示する 72 Debug.Log(www.downloadHandler.text); 73 time = www.downloadHandler.text; 74 ResultText_.GetComponent<Text>().text =time; 75 callory_time = int.Parse(time); 76 77 www=null; 78 } 79 } 80 81 82 // Update is called once per frame 83 void Update() 84 { 85 86 } 87} 88
回答1件
あなたの回答
tips
プレビュー