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); ^