Call with Retrofit method delete Yii2 which is overrided - android

I've overrided method delete in my Yii2 controller, but i can not call it from Android application with library Retrofit.
public function actionDelete($id)
{
$model=$this->findModel($id);
if($model->delete())
{
$this->setHeader(200);
echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
}
else
{
$this->setHeader(400);
echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
}
}
I tried 2 versions of calling:
first
#DELETE("posts/delete/{id}")
second
#DELETE("posts/{id}")
Call<Void> deletePosts(#Path("id") int id);
But I get error 404. So how should I correctly call this method?
When I try to call not overrided method I get error 500.
Maybe somebody knows how can I call delete method from Android to Yii2 rest?
Thanks in advance.

To pass a parameter as in public function actionDelete($id), you need to call /posts/delete?id=<id>, not just /posts/delete/<id>. However, getting this wrong should result in a HTTP 400 Bad Request error, not in a 404 Not Found error.
Possible reason for the 404 error: I assume $model=$this->findModel($id); is generated code. If so, it's likely throwing a NotFoundHttpException exception if a model with the given ID could not be found.
Another thing you can check is if a VerbFilter is configured in the behaviors() method of your controller to only allow POST requests for the delete action. You might be sending a GET request.

Related

No response called. Nor succes nor failed in loopj

In loopj when i hit a URL nor onSuccess nor onFailure calling that's why my progressDiaog keep on running for infinite time.
Make sure you are implementing the right methods (different onSuccess/onFailure methods have different inputs).
If you are implementing onSuccess with input JSONObject but your WebService is returning a JSONArray, it will not enter there.

Downloading - get a callback when received all the request headers

I use ion library for downloading files in my app. I want to read the response headers, check a particular header and either cancel or continue the download. Of course I can execute the HEAD query, check the header, and then execute the GET query. But I want to execute only one request.
Is there a way to get a callback when received all the headers, handle them and either continue or cancel the download?
Use the onHeaders callback.
.onHeaders(...)
https://github.com/koush/ion/blob/master/ion/src/com/koushikdutta/ion/builder/RequestBuilder.java#L186
I found another solution. Maybe it's better?
Ion.getDefault(<Context>).getHttpClient().insertMiddleware(new SimpleMiddleware()
{
#Override
public void onHeadersReceived(OnHeadersReceivedDataOnRequestSentData data)
{
super.onHeadersReceived(data);
}
});

Get value for volley request to finish before proceeding with method? Android Java

its my first time asking a question here, so please tell me if I missed to include something.
I have a method that prints returns a String. Inside it, is an asychronous volley request that retrieves a specific value to be added to the String. The problem is that the request is made, but since it's an asynchronous request, the method still proceeds and returns the String even if it still doesn't have the value from the request. The request completes afterwards. So the question is, is there a way to make the method wait for a specific variable to have a value before proceeding, without freezing the UI? Should I create a new thread instead?
Code below is not the actual codes I have but it shows the outline.
public String getStr{
StringBuilder sb = new StringBuilder();
String strOne;
// code here..
// asynchronous request here..
// code here..
sb.append(strOne)
// code here..
return sb.toString();
}
The asychronous request refers to the volley request.
Any help is appreciated. Thanks in advance!
You should handle the response in onResponse (#Override) method. Take a look in this tutorial:
Asynchronous HTTP Requests in Android Using Volley
If you want to return the response to another class to handle it there, I suggest you to implement an interface. I could post the code for this here, but I aswered a question like this here:
Volley , Wait for response to return wait until use flag - answer

Android Annotations: Query params gets appended to url?

#Get("/ServiceTest?version=1&dateRange={dateRange}")
ResponseModel getDateRangeTest(String dateRange);
in RestClient interface this make the following Get
http://localhost:8080/ServiceTest/2014-01-29%25202014-01-29?version=1" resulted in 400 (Bad Request); invoking error handler
Am i doing something wrong in sending the query params.
I want address this way:
/ServiceTest?version=1&dataRange=2014-01-29%25202014-01-29
Which in this case somehow i am failed to generate with Android annotations #Get
Answer: My calls were not correct and main problem was with uri encode, correct one is
/ServiceTest?version=1&dataRange=2014-01-29%202014-01-29
you might do wrong.
Your server get /ServiceTest
but you access server with address /ServiceTest/2014-01-29%25202014-01-29
Look careful that your server receive as /ServiceTest?version=1&dateRange={dateRange}
and {dataRange} is what you intend to give as variable.
Your address should be /ServiceTest?version=1&dataRange=2014-01-29%25202014-01-29
== EDIT ==
I'm not familiar with Android Get annotation, but try this.
#Get("/ServiceTest?version=1&dateStart={dateStart}&dateEnd={dateEnd}")
ResponseModel getDateRangeTest(String dateStart, String dateEnd);
and access with /ServiceTest?version=1&dataStart=2014-01-29&dateEnd=2014-01-29
Note that I change argument for more specific. and it would be a better approach.

what is the method used to fetch the cached string response using android -volley?

1)I am caching the string network response from the server using the method
request.setShouldCache(true);
2)I am trying to access the cached data using
System.out.println("the cached data"+new String(request.getCacheEntry().data));
This throws a null pointer exception .
3)I printed a output statment in HTTPHeaderparser.java .The data is printed properly .
System.out.println("the data value is"+new String(entry.data) );
4)So i guess I made some mistake while fetching the cached data .
Can some one help me out ?
The cache entry in the request it's not filled until you run the request, so you should directly call the cache to test if it contains a value with
mQueue.getCache().get(request.getCacheKey())
as bogdan put in the comment if what you want to do is get a value from cache without making a network request.
this method is fast and you can call from the UI Thread. It will return the cache entry if available even if it's expired, but you can call the isExpired() method to know if the response it's outdated.
It returns the data even if stale, because you can choose to show it while you refresh the data in the background.
Take a look at the method parseCacheHeaders(NetworkResponse) from the class HttpHeaderParser. There are some conditions there for caching the response, so the library will only cache the response if the server will provide some headers. If you want to override this, you will have to override the method parseNetworkResponse(NetworkResponse) from the Request class and call a different method for parsing the cache headers from the server.
Update
It seems I misunderstood the question a bit, for accessing Volley cache, assuming your RequestQueue instance is mQueue, you can do mQueue.getCache().get(request.getCacheKey()).

Categories

Resources