【问题标题】:How to fix Volley timeout error , while making a Request如何在发出请求时修复 Volley 超时错误
【发布时间】:2018-03-12 01:18:15
【问题描述】:

这是我第一次使用 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>

我的日志猫

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)

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

【问题讨论】:

  • 你有网络权限 ryt ??
  • 是的,我有权限
  • 可能但没有解决方案对我有用@ReazMurshed

标签: android android-volley


【解决方案1】:

我认为问题是由于这个 url 或网络连接

String server_url = "http://192.168.43.150/greetings.php";

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

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多