如何在发出请求时修复Volley超时错误

时间:2018-03-11 15:09:26

标签: android android-volley

这是我第一次使用Volley。我的程序是创建一个简单的请求。我正在我的真实设备中尝试它。我尝试从服务器检索图像,但每次仍然是相同的错误。

greetings.php

<?php
    echo "Hello from server";
?>

MainActivity.java

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

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

    textView = (TextView) findViewById(R.id.textView);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
            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 went wrong!!!");
                    error.printStackTrace();
                    requestQueue.stop();
                }
            });
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                    10000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
            ));
            requestQueue.add(stringRequest);
        }
    });
}
}

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vishal.trycreche.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:text="Server Response"
    android:textSize="20sp"
    android:id="@+id/textView"/>
<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="20dp"
    android:text="Click here"/>

</RelativeLayout>

我的logcat

03-11 19:59:05.018 2216-2532/com.example.vishal.trycreche I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err:com.android.volley.TimeoutError
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:169)
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err:at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
03-11 19:59:35.165 2216-2216/com.example.vishal.trycreche W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)

我找不到任何解决方案。 请告诉我我在哪里做错了...... 请帮帮我......提前谢谢

1 个答案:

答案 0 :(得分:0)

我认为问题是由于这个原因造成的 网址或网络连接

String server_url =“http://192.168.43.150/greetings.php”;

更改上面的网址并检查是否收到错误