2017-04-09 22 views
-2

我得到errorResponse而不是我應該得到的正常響應。我用簡單的文本創建了一個簡單的php文件,並將其放在wamp服務器上。我已經在瀏覽器上檢查了它的工作正常,但不在此代碼中。連接到WAMP時調用Android Volley onError

公共類MainActivity擴展AppCompatActivity {

Button button; 
    TextView textView; 
    String server_url="http://192.168.1.2/greetings.php"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button= (Button) findViewById(R.id.bn); 
     textView= (TextView) findViewById(R.id.txt); 


     final RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 



       StringRequest stringRequest=new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 

         textView.setText(response); 
         requestQueue.stop(); 

        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

         textView.setText("something wrong happened"); 
         error.printStackTrace(); 
         requestQueue.stop(); 

        } 
       }); 

       requestQueue.add(stringRequest); 

      } 
     }); 




    } 
} 
+0

什麼樣的錯誤? –

+0

你爲什麼使用POST請求? –

+0

[logcat會告訴你你的錯誤](http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this)。請用它編輯你的問題。 –

回答

0

我不知道你是否在模擬器或Android設備測試。 如果您使用的設備,然後檢查
1.您的設備和服務器連接到同一個網絡(例如:WiFi路由器)
2.關閉Windows防火牆保護

希望它可能會有所幫助。

+0

我也無法使用移動網絡訪問它。嘗試關閉防火牆保護,但即使這樣也行不通。 –