I want to access JSONobject
from my rest API using Volley in SDK version 29, but I am getting timeout error.
I had tried the same code before in other versions then it is working.
public class Login extends AppCompatActivity {
String data = "";
String url = "http://xyz/abc";
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button button=(Button) findViewById(R.id.lgn) ;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// making json object request
getObject();
}
});
tv=(TextView) findViewById(R.id.txtResponse);
}
public void getObject(){
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest strRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("abc", "xyz");
params.put("abc", "xyz");
params.put("abc", "xyz");
return params;
}
};
queue.add(strRequest);
//VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(req);
}
}
I want to get response of my JSONobject
using volley in SDK version 29.
This is a POST but you pass no body. Try overriding the getBody()
method as you did for getParams()
.
Something like this should work
@Override
public byte[] getBody() {
String parsedBody = new String();
try {
parsedBody = parseBody(body); /* write a function to parse the body into a String */
return parsedBody == null ? null : parsedBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", parsedBody, "utf-8");
return null;
}
}
HTTP is blocked by default since Pie (SDK level 28). Please read this for more info. https://android-developers.googleblog.com/2018/08/introducing-android-9-pie.html
Please refer to this for solution. How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?
Try to add this compileOptions in build.gradle inside android tag
android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.