-3

I am new to the android studio and trying to use Volley by repeating the code on the developer web. I have added the permission in the manifest and add 'com.android.volley:volley:1.1.1' in build Gradle. But still got an error in the last coding (queue.add(stringRequest);).

Seems add method got the problem and can not be resolved. Anyone can give a hand?

Thank you!

package com.example.mytest2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

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;

public class MainActivity extends AppCompatActivity {

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

    final TextView textView = (TextView) findViewById(R.id.text1);

    RequestQueue queue = Volley.newRequestQueue(this);
    String url ="https://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.
                    textView.setText("Response is: "+ response.substring(0,500));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            textView.setText("That didn't work!");
        }
    });

    queue.add(stringRequest);
}

Cannot resolve picture see here

error: expected queue.add(stringRequest); ^

error: expected queue.add(stringRequest); ^

Nayan Sarder
  • 512
  • 1
  • 3
  • 18

2 Answers2

0

For basic network requeest use code like below

RequestQueue requestQueue;

// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());

// Instantiate the RequestQueue with the cache and network.
requestQueue = new RequestQueue(cache, network);

// Start the queue
requestQueue.start();

String url ="http://www.example.com";

// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Do something with the response
    }
},
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // Handle error
    }
});

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

You will get proper guideline form the android devloper official site.

Nayan Sarder
  • 512
  • 1
  • 3
  • 18
0

Finally, those coding should be put inside the "onCreate"

protected void onCreate(Bundle savedInstanceState) {
     //...
    }