Volley allows to categorized network request. In this article we will go through volley string request. It takes data on server in String format and return data in String format.
As we already created RestHelper class in previous article. Which we will use in volley string request article. It looks like below.
VolleyRestHelper.java
package com.naitiks.androidvolleyexample.volleyhelper; import android.content.Context; import com.android.volley.RequestQueue;import com.android.volley.toolbox.Volley; /** * Created by Naitik on 9/24/2017. */ public class VolleyRestHelper { private static RequestQueue volleyQueue = null; private static VolleyRestHelper _this = null; private VolleyRestHelper(Context context){ volleyQueue = Volley.newRequestQueue(context); } public static VolleyRestHelper getRestHelper(Context context){ if(_this == null){ _this = new VolleyRestHelper(context); } return _this; }}
We will write one interface which will help to forward all response including error to calling method. Interface will look like this
Interface OnVolleyComplete.java
package com.naitiks.androidvolleyexample.volleyhelper; import com.android.volley.VolleyError; /** * Created by Naitik on 9/24/2017. */ public interface OnVolleyComplete { void onError(VolleyError error); void onSuccess(Object response);}
To execute volley string request we need to add some more function in this class after modification class will look likes..
Method to make volley string request
This method will be placed inside VolleyRestHelper.java
public void volleyStringRequest (int requestMethod, String reqURL, final String postData, String header, final OnVolleyComplete returnInterface){ StringRequest stringRequest = new StringRequest(requestMethod, reqURL, new Response.Listener() { @Override public void onResponse(String response) { returnInterface.onSuccess(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { returnInterface.onError(error); } }) { @Override public byte[] getBody() throws AuthFailureError { String requestBody = postData; try { return requestBody == null ? null : requestBody.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } }; _this.volleyQueue.add(stringRequest);}
You can test api http://api.phpwithtea.info/get_names.php
FragmentStringRequest.java
package com.naitiks.androidvolleyexample; import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.TextView; import com.android.volley.Request;import com.android.volley.VolleyError;import com.naitiks.androidvolleyexample.volleyhelper.OnVolleyComplete;import com.naitiks.androidvolleyexample.volleyhelper.VolleyRestHelper; /** * Created by Naitik on 9/24/2017. */ public class FragmentStringRequest extends Fragment { private View parentView = null; private Button getReq, postReq = null; private EditText postText = null; private TextView responseView = null; private VolleyRestHelper restHelper = null; private final String GET_URL = "http://api.phpwithtea.info/get_names.php"; private final String POST_URL = "http://api.phpwithtea.info/post_names.php"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { parentView = inflater.inflate(R.layout.string_request, container, false); initUI(); return parentView; } private void initUI(){ getReq = (Button) parentView.findViewById(R.id.btn_get); postReq = (Button) parentView.findViewById(R.id.btn_post); postText = (EditText) parentView.findViewById(R.id.post_data); responseView = (TextView) parentView.findViewById(R.id.info_response); getReq.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { makeGetReq(); } }); postReq.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { makePostRequest(); } }); } private void makeGetReq(){ if(restHelper == null) { restHelper = VolleyRestHelper.getRestHelper(getContext()); } restHelper.volleyStringRequest(Request.Method.GET, GET_URL, null, null, new OnVolleyComplete() { @Override public void onError(VolleyError error) { responseView.setText(error.getMessage()); } @Override public void onSuccess(Object response) { responseView.setText(String.valueOf(response)); } }); }}
string_request.xml
<!--?xml version="1.0" encoding="utf-8"?-->
I think now you have clear idea about how to work with volley string request. The above part of article explains to make GET request with volley string request.
Now we will go through volley string request using post
makePostRequest
This method will be places inside Fragment to execute post request.
private void makePostRequest(){ String postString = postText.getText().toString(); if(restHelper == null) { restHelper = VolleyRestHelper.getRestHelper(getContext()); } restHelper.volleyStringRequest(Request.Method.POST, POST_URL, postString, null, new OnVolleyComplete() { @Override public void onError(VolleyError error) { responseView.setText(error.getMessage()); } @Override public void onSuccess(Object response) { responseView.setText(String.valueOf(response)); } });}
This method will execute post volley string request.