Issue
I'm working with an API which requires me to make "URL HTMLCLient calls" and it returns a data in JSON format. I've tested the links on my browser and they work fine but when I try to retrieve the data in Android I keep getting a null value back.
Here is my code below
public String callWebservice(String weblink) {
String result = "";
try {
//String link = "http://free.worldweatheronline.com/feed/weather.ashx?q=cardiff&num_of_days=4&format=json&key=eb9390089uhq175307132201";
URL url = new URL(weblink);
URLConnection urlc = url.openConnection();
BufferedReader bfr = new BufferedReader(new InputStreamReader(
urlc.getInputStream()));
result = bfr.readLine();
} catch (Exception e) {
e.printStackTrace();
result = "timeout";
}
return result;
}
The weblink holds the url I'm trying to retrieve info, I have tested the code with the worldweatheronline JSON API and it works fine. My question here would be does the fact that the API docs have said to make URL HTMLClient calls require me to do this in a different way from a regular HTTP request if not then what could be the possible reason I am getting a null value back when the same link works fine on my broswer.
Solution
In your code you are not appending buffered output after the URL call.
This your updated code:
public String callWebservice() {
String result = "", line = "";
try {
String weblink = "http://itwillbealright.co.uk/dev1/camc/clientmethod.php?method=Login&id=&version=&username=m&password=m";
URL url = new URL(weblink);
URLConnection urlc = url.openConnection();
BufferedReader bfr = new BufferedReader(new InputStreamReader(
urlc.getInputStream()));
while ((line = bfr.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
result = "timeout";
}
return result;
}
Answered By - Ravi Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.