I have to get JSON from post url.
This is my url
URl: link
Result After process:
{
"success": false,
"error_code": null,
"data": {
"transaction_status": 0
}
}
When I hit this url, mutiple url are calling internally after a long process JSON response will display in WebView. I need to fetch this JSON response by hit this url without showing WebView
Please anybody help me out from this problem.
thanks
Add volley to your project just add the following line.
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
for get json response
private void sendRequest(){
StringRequest stringRequest = new StringRequest(JSON_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json){
// here you can get json.
}
Please follow this tutorial.
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
You need to hit webservice using HttpUrlConnection class of android API and you can get the response in json string format.
http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put
try this this will help
any more query you can ask
This url provided by OP (I mean #dev_mg99) does not consist of a "Server-side" redirect.
The JSON data on the webpage appears after "Javascript" execution.
And as much as I know doing HTTP POST/GET (using any method provided in android) will be useless in this scenario as the server is returning "webpage" and "not JSON data".
(Please correct me if I am wrong. So that I get to learn something new)
The only possible solution coming to my mind is:
1) Load the above url in a WebView
2) Keep on checking whether the WebView has loaded "completely"
3) If it has loaded completely Extract the "html" text of the WebView and obtain the desired JSON text.
No idea how to code (2)... So cannot provide any code.
How I came to know that?
Step 1:
Open above url in (say) google chrome. Wait for sometime and you will see that the page redirects to a webpage with following text:
{"success":false,"error_code":null,"data":{"transaction_status":0}}
which looks like a JSON string.
Step 2: Disable the JavaScript of the browser (For google chrome: Settings -> Show Advanced Settings -> Privacy... Content Settings --> Javascript... Click on "Do not allow any site to run JavaScript"
Step 3: Now, again, try to open above url in the browser... The page will not redirect anywhere.
According to which I conclude that the above url is redirected by JavaScript (Not from Server-Side)
Related
url: https://graph.facebook.com/100779423975829/picture?type=large;
other url with type normal: https://graph.facebook.com/100779423975829/picture?type=normal
I tried to load url with Glide, Picasso, UniversalImageLoader and decodeStream from BitmapFactory and don't have any good result and the result is the next:
UniversalImageLoader: Image can't be decoded
BitmapFactory: bitmap = null
If I open the url with Chrome pc I will automatically download the photo without opening it.
in the webview from my app, i check the redirects from this url:
https://lookaside.facebook.com/platform/profilepic/?asid=100779423975829&height=200&width=200
https://m.facebook.com/platform/profilepic/?asid=100779423975829&height=200&width=200
Anyone else is having problems loading image profiles from Facebook graphs?
Thanks!
EDIT
The bug is resolved from Facebook.
https://developers.facebook.com/bugs/261587761048160/
Finally this was a facebook bug. So the problem is fixed by themselves.
https://developers.facebook.com/bugs/261587761048160/
Yes, the redirections from http:// to https:// protocols hinders the downloading using Picasso,Glide ets. You may go ahead with following two approaches:
SimpleDraweeView from Facebook Sdk. It handles the redirection internally.
Make a graph request to fetch the re-directed url like below and use that url in picasso.
new GraphRequest(AccessToken.getCurrentAccessToken(),
"/"+fbUserId+"/picture?width="+width+"&height="+height+"&redirect=false",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
String url = response.getJSONObject().getJSONObject("data").getString("url");
loadFacebookProfilePicInImageView(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
).executeAsync();
I have the same issue. If you don't want to use the graph api for loading the public profile picture (as in my case) we both have a problem unless someone knows the solution.
I have the same problem. It seems to be a bug. I think we are best of just waiting a day or two instead of putting the effort in fixing this ourselves. See also : Facebook graph user picture won't show on mobile devices and https://developers.facebook.com/bugs/560392384345729/
Using Glide v4 and OkHttp3, how can I detect a redirection and load another url when it happens?
My usecase: I use the Glide v4 library with OkHttp3 to download pictures in my app. Sometimes when a picture is not available, a redirection is performed by the server to provide another picture instead of the one I originaly wanted. I can see it in my browser because when I request url A, I finally land on url B with the second picture. When that happens I want to instead load url C that is derived from url A (so not a static url).
At the moment I can detect the redirection using an OkHttp3 Interceptor:
public Response intercept(#NonNull Chain chain) throws IOException {
String requestUrl = chain.request().url().toString();
Response response = chain.proceed(chain.request());
String responseUrl = response.request().url().toString();
boolean redirect = !requestUrl.equals(responseUrl);
if (redirect) {
Timber.d("Detected redirection");
}
return response;
}
but then I don't know how to cleanly load url C. I don't see how I can load another url in the interceptor, and if I throw an IOException to handle the error later in a Glide RequestListener it will just result in a GlideException so I can't determine why it was throw.
OkHttp should be automatically redirecting by default and loading the final content. See OkHttpClient.Builder.followRedirects.
https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.Builder.html#followRedirects-boolean-
My own testing suggests this is working, e.g.
$ oksocial 'https://httpbin.org/redirect-to?url=https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg'
For detecting it, I assume your interceptor is registered via addInterceptor instead of addNetworkInterceptor. Is that correct?
It is also worth ensuring the redirect is via a 30X response and not via a HTML refresh.
The solution I ended up with is pretty ugly: the request interceptor I showed in the question raise an IOException when a redirection is detected. I will then use a GlideListener to detect the error, and load the url C. The ugly part is that in the GlideListener I can't determine the cause of the error, it can be a redirection but it can also be a network error or anything else.
A cleaner version of this solution can probably be achieved with a custom OkHttpUrlLoader but it's too much code for my simple usecase.
This is my solution in Glide v4 / OkHttp3
String redirect = response.header("location");
if(redirect != null && !request.url().toString().equals(redirect)) {
// if redirected your code here
}
I have a set url (ex: http://mywebsite.com/cawn28xd/user_avatar) I call for imageloading that redirects to another link that may or may not be different.
I want to be able to either intercept the 302 redirect and grab the url so the imageloader will not cache that specific url (This brings up the issue the 302 redirect url will be cached, but should be handled on the setShouldCache(false) call for the request)
OR
I want to be able to invalidate or remove caching from the specified URL using Google Volley and it's imageloader.
I am using the singleton class provided from the android developers guide including the default image loading request:
RequestEntity.getInstance(mContext).getImageLoader().get(mImageURL,
ImageLoader.getImageListener(mImageView,
R.drawable.default_avatar, R.drawable.default_avatar));
The imageloader provided from android volley uses a cachekey in order to cache requests, but during its process it makes a simple image request.
So just use a request:
Request<Bitmap> imageRequest = new ImageRequest(requestUrl, listener,
maxWidth, maxHeight, scaleType, Bitmap.Config.RGB_565, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
imageView.setImageResource(R.drawable.default_avatar);
}
});
imageRequest.setShouldCache(false);
RequestEntity.getInstance(this).getRequestQueue().add(req);
first i'm german, so sorry for my bad english ;)
Im coding an app for my school which gives the students a notification if a lesson is cancelled. My school updates the substitute plan on the internet. It's a .htm file. (http://www.dbg-filderstadt.de/fileadmin/dateien/Dokumente/w00000.htm)
So the point is that i'm new in Android/Java coding and i don't know how to get the information/source code of the website saved in a String.
Could you give me an example code how to do that?
If you want to simply make an HTTP connection to download a HTML file, you could for example use http://developer.android.com/reference/java/net/HttpURLConnection.html. You would then have to read the input stream from it and convert it to a string, and then probably also parse the HTML by some means.
However, note that having an app reading a HTML resource off the internet isn't really the normal or preferable way to do it, ideally you would want your information to be available through some kind of API. By downloading and parsing HTML your app becomes dependent on the HTML resource not changing and breaking your parsing code etc. This is really fragile.
The one way is to follow the android documentation for sending GET requests and to use the Volley library with the following code:
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String pageContent = "";
String url ="http://www.dbg-filderstadt.de/fileadmin/dateien/Dokumente/w00000.htm";
// Request a string response from the provided URL.
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.
pageContent = response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//do something
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
In this case, when your request is successful (become response code 200 Ok from the server - no errors), the system runs a method called onResponse and you become a string (named response), which is exactly the content of the page you made request to. So you could save it to your variable and use it later (for example I called this variable pageContent).
I attach you a link to the android documentation, too : https://developer.android.com/training/volley/simple.html
Do not forget to add in your AndroidManifest.xml the permissions for requesting the net:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
and the following dependency for the Volley library to your build.gradle file : compile 'com.mcxiaoke.volley:library:1.0.+'
I am trying to consume a webservice currently made in ajax. I have no idea what this web service is actually doing other than it uses POST Data. When i try to see its output on POSTMAN (Rest api client) I am getting errors.
This is the structure of web service :
var request = {};
request.UserName = "some data"; // this should be always hard coded like this
request.Password = "some data"; // this should be always hard coded like this
request.CurrentUsername = "admin"; // this is hard coded like this for now
request.FirstName = "some data";
request.LastName = "some data";
var send = {};
send.request = request;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "some link",
data: JSON.stringify(send),
dataType: "json",
async: false,
success: function (msg) {
// Process the result here
},
error: function () {
//alert("error");
// Display the error here in the front end
}
});
});
});
I need to get its output in android. Since i have little knowledge in ajax, jquery(backend) and done json parsing(in android) with post method using web service link and parameters. Please guide me how to implement in this case.
Moreover i usually check web service output on postman but here it is giving me bad request every time.
Please help.
have you tried using Fiddler4 by Telerik? i was struggling for 2 weeks at work doing something very similiar to this. I was using webclient to send data from Android to my web page. I got errors non stop. nothing online helped. using fiddler i was able to see what my post data was and where it was and all details. it should help you narrow down a lot of things.
Yes finally got a way to move it. It is very simple and following header is needed in android end thats all
httpPost.setHeader("Content-Type", "application/json");