无法通过Android Volley从php脚本获得响应

时间:2018-02-02 08:06:27

标签: php android android-volley

在Android Studio中,我尝试通过HTTP get request向PHP脚本发送Volley。我检查了PHP链接,它是有效的。但由于某种原因,我不断收到错误的响应。而不是获取PHP脚本响应。有什么想法吗?

public class FourActivity extends AppCompatActivity {
private Button bn;
private TextView txt;
private String server_url = "http://192.168.0.152/server.php";

public FourActivity() {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_four);
    bn = (Button) findViewById(R.id.bn);
    txt = (TextView) findViewById(R.id.txt);

    bn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final RequestQueue requestQueue = Volley.newRequestQueue(FourActivity.this);

            StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                             txt.setText(response);
                             requestQueue.stop();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    txt.setText("error");
                    error.printStackTrace();
                    requestQueue.stop();
                }
            }
            );
            requestQueue.add(stringRequest);


        }
    });
}

}

错误日志

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要在应用清单上添加<uses-permission android:name="android.permission.INTERNET" />权限。

希望这有帮助。