java - Android,volley : When using volley, 既没有调用 Errorlistener,也没有调用 Responselistener

标签 java android android-volley

我是 volley 和 android 的新手,我已经用 gradle 安装了 volley 并做了与官方教程相同的操作。但是没有响应,这意味着 Errorlistener 和 Responselistener 都没有被调用。 你能帮助我吗?我的代码如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.appcompat.widget.LinearLayoutCompat  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"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fuck_view"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

MainActivity.java

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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


        final TextView message = findViewById(R.id.fuck_view);

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        String url = "http://www.google.com";

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        message.setText("Response is: " + response.substring(0, 500));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                message.setText("That didn't work!");
            }
        });

        // Add the request to the RequestQueue.
        queue.add(stringRequest);
        queue.start();
       }
    }    

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.test">



    <uses-permission android:name="android.permission.INTERNET"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最佳答案

你能尝试这样的事情吗:

RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://www.google.com";

// Request a string response
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Result handling 
        System.out.println("Response is: " + response.substring(0, 500));

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

        // Error handling
        System.out.println("Something went wrong!");
        error.printStackTrace();

    }
});

// Add the request to the queue
requestQueue.add(stringRequest);

关于java - Android,volley : When using volley, 既没有调用 Errorlistener,也没有调用 Responselistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60902726/

相关文章:

java - Spring 的 AspectJ NoSuchMethodError

android - Affectiva SDK 的年龄检测

android - JakeWharton 的 DiskLruCache - 如何使用 Volley 实现?

java - Android 应用程序和服务器应该多久同步一次

android - 如何访问 Volley 中错误响应的内容?

java - 什么时候使用 System.identityhashcode() 和 hashcode() 方法?

java - 嵌套 for 循环中的手动数组复制

java - Install4j:运行批处理文件/脚本文件以进行回滚操作

android - fragment 的布局字段在初始化时为 NULL

java - Volley String 无法转换为 JSONObject