Android Read contents of a URL (content missing after in result)
I have the following code that reads the content of a url
public static String DownloadText(String url){
StringBuffer result = new StringBuffer();
try{
URL jsonUrl = new URL(url);
InputStreamReader isr = new InputStreamReader(jsonUrl.openStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null){
result.append(inputLine);
}
}catch(Exception ex){
result = new StringBuffer("TIMEOUT");
Log.e(Util.AppName, ex.toString());
}
in.close();
isr.close();
return result.toString();
}
The problem is I am missing content after 4065 characters in the result returned. Can someone help me solve this problem.
Note: The url I am trying to read contains a json response so everything is in one line I think thats why I am having some content missing.
Asked by: Steven782 | Posted: 24-01-2022
Answer 1
Try this:
try {
feedUrl = new URL(url).openConnection();
} catch (MalformedURLException e) {
Log.v("ERROR","MALFORMED URL EXCEPTION");
} catch (IOException e) {
e.printStackTrace();
}
try {
in = feedUrl.getInputStream();
json = convertStreamToString(in);
}catch(Exception e){}
while convertStreamToString is:
private static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Answered by: Kimberly373 | Posted: 25-02-2022
Answer 2
Here is a cleaner version to fetch the output of a script from the web:
public String getOnline(String urlString) {
URLConnection feedUrl;
try {
feedUrl = new URL(urlString).openConnection();
InputStream is = feedUrl.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "");
}
is.close();
return sb.toString();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
And remember that you cannot download anything from the main thread. It has to be from a separate thread. Use something like:
new Thread(new Runnable(){
public void run(){
if(!isNetworkAvailable()){
Toast.makeText(getApplicationContext(), getResources().getString(R.string.nointernet), Toast.LENGTH_LONG).show();
return;
}
String str=getOnline("http://www.example.com/script.php");
}
}).start();
Answered by: Adelaide923 | Posted: 25-02-2022
Similar questions
httpwebrequest - Android HttpPost data
I am trying to send data to my server using HttpPost via the following code.
private boolean FacebookLogin(String url) {
boolean isDataSend = false;
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("data", FacebookData()))...
httpwebrequest - Symfony2 basic http auth, works with curl but not on Android
I'm trying to authenticate a user on my symfony2 server.
The authentication works with curl in cli :
curl "http://localhost:8080/app_dev.php/app/user/profile.json?id=4" -u foo:bar --basic
But with Android i always get a 401 error with this error:
A Token was not found in the SecurityContext.
What can I do to fix it ?
Here is my security.yml:
...
httpwebrequest - How to obtain result from a website using Http in android
I want to use an external website http://www.siirretytnumerot.fi/ in my android application. This website takes in two values PREFIX and NUMBER. I am confused at the moment as I seem not to be getting any output in my textview. I dont know what to use whether httpget or httppost. I tried both and still no result. But when i go to the link for explorer and enter a...
httpwebrequest - Sending HTTP DELETE request in Android
My client's API specifies that to remove an object, a DELETE request must be sent, containing Json header data describing the content. Effectively it's the same call as adding an object, which is done via POST. This works fine, the guts of my code is below:
HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setUseCaches(false);
con.con...
httpwebrequest - Can we store all the http/https request of my phone using android app?
Can we store all the http/https request of my phone using android app?
Is there any way to store the traffic of my android app?
httpwebrequest - httpUnit parse HTML in JAVA Android application Unable to resolve superclass
I'm trying to login to a website (using POST) and parsing some HTML. The login works (using jsoup), but when I try to parse a full page, the Inputstream buffer is not big enough to capture the entire page.
public String Test() throws Exception {
Log.d(TAG, "-->>> STARTING TEST");
WebConversation wc = new WebConversation();
WebRequest req = new GetMethodWebRequest( "http://www.meter...
httpwebrequest - Android HTTP Request for CakePHP URL
I have a website which was created with cakephp. I want to pass some values formed in my App to this website. When I enter the exact same URL in the browser it works.
The URL is something like: www.something.com/function/add/value
So I'm very confused if this is a GET or a POST method? And how I can do that?
The thing is that I can NOT change this URL or put some POST or GET PHP script there to get ...
httpwebrequest - Android: Http request doesn't work on 4.0
I tested this code and it works fine on 2.2 and 2.3.3, but it crashes on 4.0.
The problem seems to be with the http request. Any ideas why?
public class Rezultat extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//http post
InputStream is=null;
try{
String url="http:/...
httpwebrequest - Base way to handle multiple http calls simultaneously in android
I am creating a social app. In this there are many http calls. I have create a method to handle which is used for http calls. I have created saprate AsyncTask for each request. My problem is the responce is very slow if there is multiple http requests send simultaneously.
Please tell me how to resolve this problem (what is the best way to handle multiple Http request at same time).
android - Mono httpwebrequest turns wifi off
I have an Mono/Android app with a background service that makes some call to a rest webservice via httpwebrequest. On some devices (a samsung galaxy s4 and s4 mini) the requests begins to hang, then timeout and after some minutes the wifi of the phone is turned off (I suggest is the OS) .
Closing the app the wifi returns available, anybody knows why?
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android