Launch specific method from argument/parameter in Java - java

I would like to launch a specific method from an argument/parameter of another method in Java.
Consider following code:
void getResponse(String getUrl, final Activity activity) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(getUrl)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
//txtString.setText("First Name: "+json.getJSONObject("data").getString("first_name") + "\nLast Name: " + json.getJSONObject("data").getString("last_name"));
ReturnedString(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
The public void run() launches ReturnedString(json.toString());
But I would like to be "ReturnedString" a parameter of the getResponse method, so I can reuse getResponse.
This would mean that getResponse becomes something like:
void getResponse(String getUrl, final Activity activity, Method method) throws IOException {
And
method(json.toString());
But it seems not to be working this way.
Albert

You can use java.util.function.Consumer as an parameter of the method.
void getResponse(String getUrl, final Activity activity, final Consumer<String> consumer) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(getUrl)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
//txtString.setText("First Name: "+json.getJSONObject("data").getString("first_name") + "\nLast Name: " + json.getJSONObject("data").getString("last_name"));
//ReturnedString(json.toString());
consumer.accept(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
and the call of this method would be:
getResponse(url, activity, new Consumer<String>(){
#Override
public void accept(String s) {
ReturnedString(s);
}
});

You can use a Function<String, Void> as third parameter:
Initialize it as follows:
import androidx.arch.core.util.Function;
Function<String, Void> function = new Function<String, Void>() {
#Override
public Void apply(String input) {
ReturnedString(input);
}
}
Your method with the Function parameter:
void getResponse(String getUrl, final Activity activity, final Function<String, Void> function) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(getUrl)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
function.apply(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
Let's assume you need another Function, say
Function<String, Void> otherFunction = new Function<String, Void>() {
#Override
public Void apply(String input) {
Log.i("TEST", input);
}
}
Then you can use the two Functions as follows:
for(String day: daysOfWeek){
if(itIsTuesday(day)){
getResponse(urlString, activity, function);
}
else{
getResponse(urlString, activity, otherFunction);
}
}

Related

Method get call twise while in Thread. I am trying to call socket

Here i am calling socket with runnable interface.but the socket is continue running thread and displaying multiple msg with same id.Not getting idea where it goes wrong.
Here is Code.
Request request = new Request.Builder().url(webSocketApi).build();
EchoWebSocketListener listener = new EchoWebSocketListener();
ws = client.newWebSocket(request, listener);
ws.send(data);
client.dispatcher().executorService();
** EchoWebSocketListener **
class EchoWebSocketListener extends WebSocketListener {
#Override
public void onOpen(WebSocket webSocket, okhttp3.Response response) {
super.onOpen(webSocket, response);
}
#Override
public void onMessage(WebSocket webSocket, String message) {
new Thread(new Thread1(message)).start();
}
#Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
Log.d("LL onMessage", "Receive Bytes : " + bytes.hex());
}
#Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(CLOSE_STATUS, null);
Log.d("LLonClosing", "Closing Socket : " + code + " / " + reason);
}
#Override
public void onFailure(WebSocket webSocket, Throwable throwable, okhttp3.Response response) {
super.onFailure(webSocket, throwable, response);
Log.d("LL", "Error : " + throwable.getMessage());
}
}
Thread class
where response need to show in recyclerview.Here socket call twise and getting same messages.
class Thread1 implements Runnable {
private String u_message;
Thread1(String u_message) {
this.u_message = u_message;
}
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
System.out.println("thread1 server message" + u_message);
try {
Gson gson = new Gson();
SocketResponse p = gson.fromJson(u_message, SocketResponse.class);
socketResponses.add(p);
adapter = new ChatAdapter(context, socketResponses);
rvRecycler.setAdapter(adapter);
start(p.getOwnerId(), p.getRoomId(), abc);
Log.d("TAG", "run: Yes");
} catch (
JSONException e) {
e.printStackTrace();
}
}
});
}
}

Move method to another Java class, and call the method from the original class

Dear valued program gurus!
I need help on moving a method to another Java class.
I am having a Java class called ProfileList.java containing the following code:
package dk.timeleft.versionone;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class ProfileList extends AppCompatActivity {
Button btnGet;
Button btnPost;
TextView txtResult;
public String url;
public String postUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_list);
btnGet = (Button) findViewById(R.id.btnGet);
btnPost = (Button) findViewById(R.id.btnPost);
txtResult = (TextView) findViewById(R.id.txtResult);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtResult.setText("Retrieving GET-data");
url = "https://kairosplanner.com/api/timeleft.php";
try {
getResponse();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnPost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtResult.setText("Retrieving POST-data");
postUrl = "https://kairosplanner.com/api/timeleft2.php/";
RequestBody postBody = new FormBody.Builder()
.add("first_name", "Hans")
.add("last_name", "Schmidt")
.build();
try {
postRequest(postUrl, postBody);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
void postRequest(String postUrl, RequestBody postBody) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
ProfileList.this.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
//txtString.setText("First Name: "+json.getJSONObject("data").getString("first_name") + "\nLast Name: " + json.getJSONObject("data").getString("last_name"));
txtResult.setText(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
void getResponse() throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
ProfileList.this.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
//txtString.setText("First Name: "+json.getJSONObject("data").getString("first_name") + "\nLast Name: " + json.getJSONObject("data").getString("last_name"));
txtResult.setText(json.toString());
Toast.makeText(ProfileList.this,"Hello",Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
public class OkHttpHandler extends AsyncTask<String, Void, String> {
OkHttpClient client = new OkHttpClient();
#Override
protected String doInBackground(String... params) {
Request.Builder builder = new Request.Builder();
builder.url(params[0]);
Request request = builder.build();
try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//txtString.setText(s);
}
}
}
This is working without any problems, but I'd like to have my code clean an neat.
I will be using the POST and GET functions (postRequest and getResponse methods) often, also in other Java classes, so it would be better, I guess, if those methods, including the OkHttpHandler class, to a separate Java class, e.g. ApiCommunicator.java, and call the methods from there.
I found a lot of information on how to refactor, but that just deletes the current ProfileList.java class.
I also tried just to copy the methods (postRequest, getResponse and OkHttpHandler to ApiCommunicator.java (and afterwards delete these methods from ProfileList.java), but that gives a few other problems, e.g. the .runOnUiThread runnable within the OnResponse method in postRequest and getResponse - those refer to ProfileList.this in stead of a dynamic Java class.
So my question is: how do I move a method from one class to another, and call the method from the original class?
BTW: I am using IntelliJ
I hope somebody can help me with this problem.
EDITED: Added ApiCommunicatorListener interface to ApiCommunicator that needs to be implemented by ProfileList activity to get the result back and assign it to txtResult.
You could create your ApiCommunicator class like so:
public class ApiCommunicator<T extends AppCompatActivity & ApiCommunicator.ApiCommunicatorListener> {
private T activity;
public ApiCommunicator(T activity) {
this.activity = activity;
}
public void postRequest(String postUrl, RequestBody postBody) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
activity.postRequestResult(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
public void getResponse() throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
final String myResponse = response.body().string();
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
JSONObject json = new JSONObject(myResponse);
activity.getResponseResult(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
public interface ApiCommunicatorListener {
void postRequestResult(String result);
void getResponseResult(String result);
}
}
After that, you need to implement the interface in ProfileList activity like so:
public class ProfileList extends AppCompatActivity implements ApiCommunicator.ApiCommunicatorListener {
Then you're gonna add those two methods to ProfileList:
#Override
public void postRequestResult(String result) {
txtResult.setText(result);
}
#Override
public void getResponseResult(String result) {
txtResult.setText(result);
}
And finally use ApiCommunicator:
ApiCommunicator apiCommunicator = new ApiCommunicator<>(this);
apiCommunicator.postRequest(...);
apiCommunicator.getRequest(...);

Okhttp3 - IndexOutOfBoundsException after calls to API

I'm new to android development but I'm stuck on why I can make calls to my API, but it doesn't populate my class in time for the recycler view to populate. I get IndexOutOfBoundsException because the mData.getDataFeeds() returns null. If I debug this application and walk through it slowly, it works.
ListFeedAdapter listFeedAdapter = new ListFeedAdapter(mData.getDataFeeds());
I have an Activity that gets a Fragment.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
try {
login();
getFeed();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
ListFeedAdapter listFeedAdapter = new ListFeedAdapter(mData.getDataFeeds());
recyclerView.setAdapter(listFeedAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
return view;
}
Then I call login()
private void login() throws IOException {
String user = "user";
String password = "pass";
String loginUrl = getString(R.string.jsonLogin);
OkHttpClient client = new OkHttpClient.Builder()
.build();
JSONObject credentials = new JSONObject();
JSONObject session = new JSONObject();
try {
credentials.put("email", user);
credentials.put("password", password);
session.put("session", credentials);
} catch (JSONException e) {
e.printStackTrace();
}
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, session.toString());
Request request = new Request.Builder()
.url(loginUrl)
.post(body)
.addHeader("Content-Type", mediaType.toString())
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
String jsonData = response.body().string();
String jsonHead = response.headers("Set-Cookie").toString();
if(response.isSuccessful()) {
for (String setCookie : response.headers("Set-Cookie")) {
cookies.add(Cookie.parse(response.request().url(), setCookie));
}
}
}
});
The getFeed()
private void getFeed() throws IOException, JSONException {
String loginUrl = "http://testurlhere";
OkHttpClient client = new OkHttpClient.Builder()
.build();
MediaType mediaType = MediaType.parse("application/json");
Request request = new Request.Builder()
.url(loginUrl)
.get()
.addHeader("Content-Type", mediaType.toString())
.addHeader("_session", cookies.get(0).toString())
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonData = response.body().string();
String jsonHead = response.headers("Set-Cookie").toString();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mData = parseDataFeed(jsonData);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
updateDisplay();
}
});
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
okhttp is an asynchronous operation, and you should use mData after onResponse ()
call.enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonData = response.body().string();
String jsonHead = response.headers("Set-Cookie").toString();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mData = parseDataFeed(jsonData);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
ListFeedAdapter adapter = new ListFeedAdapter(mData.getDataFeeds());
rexyxlerView.setAdapter(adapter);
updateDisplay();
}
});
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});

HTTPPOST Android with Body

I was looking for an easy way to make an HTTP post in Android with body, my api call should be like :
https:url/api/message?token=myToken&channel=Pew&text=someText&username=User
What I did is this, I created this class
Public class ApiCalls {
private static PostCommentResponseListener mPostCommentResponse;
private static Context mContext;
public ApiCalls(){
}
public static void postNewComment(Context context, final String message){
mContext = context;
String apiUrl = context.getResources().getString(R.string.api_url);
mPostCommentResponse.requestStarted();
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST,apiUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
mPostCommentResponse.requestCompleted();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mPostCommentResponse.requestEndedWithError(error);
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("token",mContext.getResources().getString(R.string.access_token));
params.put("channel","pew");
params.put("text", message);
params.put("username","User");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
}
public interface PostCommentResponseListener {
public void requestStarted();
public void requestCompleted();
public void requestEndedWithError(VolleyError error);
}
}
But it doesn't work, it only shows app has stopped.
Is good to use Volley? Or you recommend to me to use other way? I used to use HttpClient but it's deprecated now...
What I'm missing?
Log error
java.lang.NullPointerException: Attempt to invoke interface method 'void com.package.ApiCalls$PostCommentResponseListener.requestStarted()' on a null object reference
You can send json body using volly as below two ways.
1. Using JsonObjectRequest
Map<String,String> params = new HashMap<String, String>();
params.put("token",mContext.getResources().getString(R.string.access_token));
params.put("channel","pew");
params.put("text", message);
params.put("username","User");
JsonObjectRequest request_json = new JsonObjectRequest(URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
//Process success response
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// handle error
}
});
// add the request object to the queue to be executed
queue.add(request_json);
2. Using JSON directly in request body
JSONObject jsonBody = new JSONObject();
jsonBody.put("token",mContext.getResources().getString(R.string.access_token));
jsonBody.put("channel","pew");
jsonBody.put("text", message);
jsonBody.put("username","User");
final String mRequestBody = jsonBody.toString();
StringRequest sr = new StringRequest(Request.Method.POST,apiUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Process success response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// handle error
}
}){
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody.getBytes("utf-8");
} catch (Exception e) {
return null;
}
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
// add the request object to the queue to be executed
queue.add((sr);
Initialise your mPostCommentResponse like this:
public ApiCalls(){
mPostCommmentResponse = (PostCommentResponseListener)mContext;
}
it will do you work and rest is fine. Thanks.
EDITED:
In Another Activity from where you want to call "ApiCall" class, do code like that:
new ApiCalls().postNewComment(AnotherActivity.this,"Your Messsage here");
and in method "postNewComment" do like this:
mContext = context;
mPostCommmentResponse = (PostCommentResponseListener)mContext;
Is it ok and understable??

Get Android Volley response in another class

I'm trying to use Volley as a DBA layer to call a webservice that hadles JSON objects. Because this layer is below the activity and another service layer, it doesn't seem to be working properly. I'll try to explain my setup:
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProductService productService = new ProductService();
productService.getProduct();
}
ProductService.java:
public void getProduct() {
JsonObjectRequest req = new JsonObjectRequest("http://echo.jsontest.com/name/Milk/price/1.23/", null, createMyReqSuccessListener(), createMyReqErrorListener());
ApplicationController.getInstance().addToRequestQueue(req);
}
private Response.Listener<JSONObject> createMyReqSuccessListener() {
return new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.v("response", response.toString());
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
return;
}
};
}
I hope that is clear enough.
In the end, I would like to use the ProductService::getProduct() from an activity and the the actual JSON response from the webservice in a variable which I can later use.
However, at the moment, the line
Log.v("response", response.toString());
doesn't even execute. What am I doing wrong?
What I would try is this:
Declare getProduct as
public void getProduct(Response.Listener<JSONObject> listener,
Response.ErrorListener errlsn) {
JsonObjectRequest req = new JsonObjectRequest("http://echo.jsontest.com/name/Milk/price/1.23/",null, listener, errlsn);
ApplicationController.getInstance().addToRequestQueue(req);
}
And than call in your activity like this:
productService.getProduct(
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
variableFromActivity = response;
//Or call a function from the activity, or whatever...
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Show error or whatever...
}
});
Create an abstract class AppActivity
import androidx.appcompat.app.AppCompatActivity;
public abstract class AppActivity extends AppCompatActivity
{
abstract void callback(String data);
}
Extend all your Activities using AppActivity
public class MainActivity extends AppActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "Your URL";
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("Title", "Android Volley Demo");
jsonBody.put("Author", "BNK");
}
catch (JSONException e) {
System.out.println(e);
}
final String requestBody = jsonBody.toString();
Messenger messenger = new Messenger(MainActivity.this);
messenger.sendMessage(this, url, requestBody);
}
public void callback(String data)
{
System.out.println(data);
}
}
Create Messenger class as below:
public class Messenger
{
private AppActivity myActivity;
public Messenger(AppActivity activity)
{
myActivity = activity;
}
public void sendMessage(Context context, String url, final String requestBody)
{
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
// Request a string response from the provided URL.
StringRequest stringRequest =
new StringRequest(
Request.Method.POST,
url,
null,
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
}
) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
}
catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{
myActivity.callback(new String(response.data));
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
queue.add(stringRequest);
}
}
Hope it helps.

Categories