2017-04-09 56 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

我也无法使用移动网络访问它。尝试关闭防火墙保护,但即使这样也行不通。 –