I have an android application that communicates with WebService
. My application sends request to Web Service which is written in php
. The php side, just processes the request and echo
out result in JSON
format.
Although amount of data being transferred is minimal, sometimes it takes too long to load data in my app.
Here are methods in my android app that are responsible for Sending the request and retrieving response
/**
* Posts data to server.
*
* @param url Server URL
* @param json {@link JSONObject} to be sent
* @param tag Tag. used to describe the request
* @return {@link HttpResponse}
* @throws IOException
* @throws ClientProtocolException
*/
public HttpResponse postData(String url, JSONObject json, String tag)
throws ClientProtocolException, IOException {
SharedPreferences prefs = context.getSharedPreferences(SUPPORTDESK, Context.MODE_PRIVATE);
HttpResponse httpResponse = null;
String jsonString = json.toString();
String encodedURL = URLEncoder.encode(jsonString, HTTP.UTF_8);
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("username", prefs.getString(USERNAME_KEY, "")));
value.add(new BasicNameValuePair("password", prefs.getString(PASSWORD_KEY, "")));
value.add(new BasicNameValuePair("tag", tag));
value.add(new BasicNameValuePair("data", encodedURL));
DefaultHttpClient httpClient = new DefaultHttpClient();
//setTimeout(httpClient, 5000);
HttpPost request = new HttpPost(url);
// StringEntity entity = new StringEntity(jsonString);
// request.setHeader("Content-Type", "application/json");
// request.setEntity(entity);
request.setEntity(new UrlEncodedFormEntity(value, HTTP.UTF_8));
httpResponse = httpClient.execute(request);
return httpResponse;
}
/**
* Retrieves String representation of {@link HttpResponse}
*
* @param response {@link HttpResponse}
* @return <b>String</b> Actual Response from server as string.
* @throws IOException
* @throws IllegalStateException
* @throws UnsupportedEncodingException
*/
public static String getServerResponse(HttpResponse response)
throws IOException, IllegalStateException,
UnsupportedEncodingException {
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,
HTTP.UTF_8), 100);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String out = URLDecoder.decode(sb.toString().trim(), HTTP.UTF_8).trim();
int ascii = (int) out.charAt(0);
return (ascii >= 33 & ascii <= 126)?out:out.substring(1);
}
postData
method will send the request and waits for the response, the response then will pass to getServerResponse
to retrieve string representation of the response. And of course all long-Running
codes are wrapped in AsyncTask
class.
and here is a sample method of my web service:
function transactionDetail($json) {
$array = json_decode($json, TRUE);
$db = new DBHelper();
$transactionId = $array[$db->JS_RP_TRANSACTION_ID];
$response = $db->getTransactionDetails($transactionId);
echo urlencode(json_encode($response));
}
So, is the method i'm using on Android Side for making the request and retrieving the response, Standard way to do it, or there is a better way ?
How can i set timeout for the request i make ?, you can see a commented line in my code, i tried to set a timeout but it broke the code.
And is the combination of Client/Server side
code i'm using good enough or there is more standard or improved way to achieve this that i dont know of ?