Reading large response body as HttpClient - java

I'm building an android application where I have to read a big chunk of information from a server.
The server sends the information as a JSONArray and everything is fine on the server side.
However the problem comes when I'm trying to receive this JSONArray as it seems to be too large for me to receive?
I connect to the server using HttpClient like this:
HttpParams httpParams = new BasicHttpParams();
int timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
int timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
HttpClient client = new DefaultHttpClient(httpParams);
HttpGet request = new HttpGet();
request.setURI(url);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
if (response != null) {
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 100000);
}
return in;
And I simply try to read what's in the BufferedReader with the readLine() method, however what I get back is not the full content of the response. I know this because I tried accessing the same content with a web browser and it returns the full content.
The response from the server is on a single line (might affect it, I don't know)
I'm able to read 4kb and I need to read a whopping 141kb (so I'm far off)..
My personal guess is that somewhere in my code there is a size limit.
Extra I read from the BufferedReader like this:
BufferedReader in = getResponse(new URI(params[0]));
// Read from the received buffer
String s;
StringBuilder sb = new StringBuilder(10000);
if(in != null){
while ((s = in.readLine()) != null){
sb.append(s);
}
}
Log.d("Problem: ", sb.toString());
EDIT: The response does not differ, it's always the same length
EDIT2: It seems that it was Log.d() that had a limit in print length! There was no problem to begin with... sorry!

Use Gzipped JSON in HTTP Request
Checkout this article: http://arnab.ch/blog/2012/09/android-how-to-send-gzipped-json-in-http-request/

Instead of reading the InputStream just directly convert it to String using EntityUtils.toString
example:
try {
response = client.execute(request);
HttpEntity resEntityGet = responseGet.getEntity();
final String result = EntityUtils.toString(resEntityGet); //result is your json response
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Related

Passing data between android and server using JSON parsing

I'm trying to send and receive data between android app and server using JSON but unfortunately i am unable to do it i searched a lot but it didn't help. My code is as follows:
In android
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("fromDate", from);
jsonObject.put("toDate", to);
jsonObject.put("reason", reas);
new SendData().execute(jsonObject);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and then my asynctask's doInBackground method
try {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
CmsInter.TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, CmsInter.TIMEOUT);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(getResources().getString(
R.string.URL));
StringEntity se = new StringEntity(object.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httpPost.setEntity(se);
HttpResponse response = client.execute(httpPost);
message = Sync.convertResponseToString(response);
} catch (Exception e) {
e.printStackTrace();
message = e.toString();
}
and then on server side within the doPost() method of servlet code is as follows :
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
StringBuffer sb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
sb.append(line);
} catch (Exception e) { /* report an error */
e.printStackTrace();
}
String str = sb.toString();
try {
org.json.JSONObject json = new org.json.JSONObject(str);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and I think this is not the right way to read JSONObject on the server side because when I run this I get org.json.JSONException: A JSONObject text must begin with '{' at character 1 exception , Can anyone please help me out in this. Any help will be appreciated. Thanks.
Finally got it this line was wrong object.toString() while sending it should have been object[0].toString() because asynctask's doInBackground methods parameter is an array i.e. protected String doInBackground(JSONObject... object).

google-api-client to create short URL in java

I have a link that is way too long (got some URL-paramameters etc). I want to shorten this using the googpe API urlshortener.
The API-key is created in google Developers Console. The key is an 'public API access' and a 'Key for server applications'.
Can anyone see why this code does not work? I have tried for way too long to make this happen.
try {
String g = "https://www.googleapis.com/urlshortener/v1/url";
String url = g + "?key=secretKey";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
// add header
//post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept", "application/json");
//add the long url as a parameter
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("longUrl", "www.google.com"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
} } catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Returns a responsecode of 400
I guess there was just a problem with my HTTP-request. When I used the code used here: http://www.snip2code.com/Snippet/216067/Call-Google-Shorten-URL-API it worked.
Posting this answer instead of just deleting the question, because the API is recently changed (I guess mid-2014), so there is not much updated examples of this.
Good luck to all of you trying to access this API :)

Android: Uploading Strings To Server

I'm trying to let my users be able to report small errors my android application automatically catches to my server. It's nothing big, just a small text box and send button.
It's supposed to send 3 strings (error, optional user description, and time) to a file on my website made specifically to capture those errors. The thing is, I have absolutely no idea how to do so. I only know how to let my application read info from my website but not the other way around.
Do I have to have a special script on my website? Are JSON Strings necessary? I need the string to be saved there. (Not temporarily)
I'm a bit of a newbie so any help is appreciated. Thanks!
- There has to be a script running on your server, eg: php script.
- Its actually a web-service published on the server so that a client can access it.
- Then you will need to do a HTTP Post to the Server, its better to use NameValuePair along with it to send the data.
This is my code for doing HTTP POST:
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = MySSLSocketFactory.getNewHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now", sb + "");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} /*
* catch (ParserConfigurationException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } catch
* (SAXException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method " + sb.toString());
return sb.toString();
}
//////////////////////////// Edited Part ///////////////////////////////////
Server side php code:
<?php
require_once(ROOT.'/lab/lib/xyz_api_int.php');
try {
//setup the sdk
$api = YumzingApiInt::_get(
Config::get('api_int','url'),
Config::get('api_int','key'),
Config::get('api_int','secret')
);
//connect to the api
$api->connect();
//check our token
echo $api->getToken();
} catch(Exception $e){
sysError($e->getMessage());
}
You just need to post values by http to a php script on your server that will save those values in your file or a database.

Java application terminates at getOutputStream()

I'm creating an application for our Android devices. The aim of this section is to post a username and password (currently just assigned as a string) to a web service and to receive a login token. When running the code, at the getOutputStream() line, my code terminates and will no progress any further.
I have assigned the android emulator GSM access and also set the proxy and DNS server within Eclipse. I'm not sure where to go with it now!
This is within my onHandleIntent():
protected void onHandleIntent(Intent i) {
try{
HttpURLConnection http_conn = (HttpURLConnection) new URL("http://www.XXXXX.com").openConnection();
http_conn.setRequestMethod("POST");
http_conn.setDoInput(true);
http_conn.setDoOutput(true);
http_conn.setRequestProperty("Content-type", "application/json; charset=utf-8");
String login = URLEncoder.encode("XXXXX", "UTF-8") + "=" + URLEncoder.encode("XX", "UTF-8");
login += "&" + URLEncoder.encode("XXXXX", "UTF-8") + "=" + URLEncoder.encode("XX", "UTF-8");
OutputStreamWriter wr = new OutputStreamWriter(http_conn.getOutputStream());
//TERMINATES HERE
wr.write(login);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(http_conn.getInputStream()));
String line = rd.toString();
wr.close();
rd.close();
http_conn.disconnect();
}
catch (IOException e){
}
}
This is my first go at java and have only been writing it for a few days so bear with me if I've missed something obvious.
Thanks
If you want to POST something using HTTP, why not use HTTP POST? ;-)
Here is an example snippet:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Source: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
This may not be the appropriate answer, but will certainly be helpful to you. I have used this code for sending and receiving the request and reply resp, to a webservice.
This code is working, but will need some Refactoring, as i have used some extra variable, which are not needed.
I have used the NameValuePair here for Post
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now",sb+"");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} /*
* catch (ParserConfigurationException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } catch
* (SAXException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method "+sb.toString());
return sb.toString();
}
String line = rd.toString();
should be
String line = rd.readLine();
that might do the trick. rd.toString() gives you a String representation of your BufferedReader. It does not trigger the HTTP operation. I did not test your code, so there might be other errors as well, this was just the obvious one.

Retrieving Google calendar list using gdata-android-2.2.1-alpha

I need a simple working example with all the relevant import.
I found only one example but its too complicated and many imports are missing
Okay I've found a better solution I written this using HttpGet
/***************************************************************************************************************************/
/* RETURNS Calendar List */
/***************************************************************************************************************************/
//#param authKey, represents the token key ,Auth=......
public void calendarList(String authKey){
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://www.google.com/calendar/feeds/default/allcalendars/full");
httpget.setHeader("Authorization","GoogleLogin "+authKey);
// Execute HTTP Post Request
org.apache.http.HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity httpEntity = response.getEntity();
InputStream stream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
Log.d("RESPONSE LIST",line+"\n");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Categories