I have an android app that most of its feature consumes an API. My client complained that the retrieving of data from the web api is very slow. I wonder what's causing this.
Here's a basic structure of how I do my calls:
String returnString = "";
token = tokenBuilder.generateToken("Customers/All");
try {
HttpGet request = new HttpGet(apiUrl + "CustomerRewards/All?customerId=" + id);
request.setHeader(HTTP.CONTENT_TYPE, "application/json");
request.setHeader("AccountId", headerAccountId);
request.setHeader("StoreId", headerStoreId);
request.setHeader("AppKey", headerAppKey);
request.setHeader("Token", token);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
String responseString = EntityUtils.toString(response.getEntity());
Log.i("Api Message", responseString);
returnString = responseString;
} catch (Exception e) {
returnString = e.getMessage();
}
return returnString;
I'm calling this method from a progress dialog in order to display a loader while, retrieving data from the web API. Is there a better way to do this? Or is somewhat my android code affects its performance?
Here's the code on the calling progress dialog.
rewardsErrorMessage = "";
progressBar = new ProgressDialog(this);
progressBar.setCancelable(true);
progressBar.setMessage("Loading info ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.setCancelable(false);
progressBar.show();
apiLoadStatus = false;
new Thread(new Runnable() {
public void run() {
while (!apiLoadStatus) {
apiLoadStatus = RewardApi();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (apiLoadStatus) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}}).start();
progressBar.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
SetValues();
}
});
And here's the method that actually calls the api class that connects to the web api
ApiConnection api = new ApiConnection();
try {
Log.i("CustomerId", customerInfos.getString("customer_id", ""));
transactionMessage = api.GetTransactions(customerInfos.getString("customer_id", ""));
availableRewardsMessage = api.GetCustomerRewards(customerInfos.getString("customer_id", ""));
try
{
if(transactionMessage.contains("Timestamp"))
{
Log.i("Transaction", "Success");
GetPoints(transactionMessage);
}
if(!availableRewardsMessage.equals("[]") && !availableRewardsMessage.equals("[]"))
{
JSONObject rewardsJson = new JSONObject(availableRewardsMessage);
availableRewardsMessage = rewardsJson.getString("AvailableRewards");
hasRewards = true;
}
return true;
}
catch(Exception e)
{
rewardsErrorMessage = transactionMessage.replace('[', ' ').replace(']', ' ').replace('"', ' ');
}
} catch(Exception e) {
e.printStackTrace();
rewardsErrorMessage = e.getMessage();
return true;
}
return true;
As you notice, I have two api calls here.
I really would like to speed up the retrieving of data. Or is the api I'm consuming that slow? Any ideas guys? Thanks!
As you're probably aware, there are a number of factors that can affect HTTP service calls, of which the client code is only one:
Network speed and latency
Server availability and load
Size of data payload
Client device resources
Client code
You really need to determine where the bottleneck(s) are in order to know where to try to optimize. Additionally, you should make sure that the server is using Gzip to compress the payload and add the following to your client code:
request.setHeader("Accept-Encoding", "gzip");
Related
How to make a PUT request using HttpURLConnection with query parameters?
I am trying to consume a third party REST API using HttpURLConnection but when I try to pass the parameters in the URL, it doesn't work and throw an error as shown below:
The REST API Url could not be found in the mappings registry
This is the code block that doesn't work for me as of now:
URL url;
StringBuffer response = new StringBuffer();
try
{
url = new URL(" http://thirdparty.com/party/api/v2/ksp/12/ks");
HttpURLConnection httpURL = (HttpURLConnection) url.openConnection();
httpURL.setDoOutput(true);
httpURL.setRequestMethod("PUT");
StringBuilder sbUrl = new StringBuilder("parameter1_id=");
sbUrl.append(getParameter1Value())
.append("¶meter2_id=")
.append(getParameter2Value());
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURL.getOutputStream()));
writer.write(sbUrl.toString());
writer.flush();
writer.close();
// throw the exception here in case invocation of web service
if (httpURL.getResponseCode() != 200)
{
// throw exception
}
else
{
//SUCCESS
}
}
catch (IOException e)
{
}
When I provide these parameters in the Body as form-data parameters, the REST API seems provide the response.
My question here is that how do I make this work with HttpURLConnection?
What have I tried till now?
I have tried to modify the above to something like below, but it doesn't work.
try
{
url = new URL(" http://thirdparty.com/party/api/v2/ksp/12/ks");
HttpURLConnection httpURL = (HttpURLConnection) url.openConnection();
httpURL.setDoOutput(true);
httpURL.setRequestMethod("PUT");
httpURL.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + "----WebKitFormBoundarydklhfklsdfhlksh");
dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"parameter1_id\"");
dataOutputStream.writeBytes("\r\n" + "parameter1Value" +"\r\n");
dataOutputStream.writeBytes("--" + "----WebKitFormBoundarydklhfklsdfhlksh");
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"parameter2_id\"");
dataOutputStream.writeBytes("\r\n" + "parameter2Value" + "\r\n");
dataOutputStream.writeBytes("--" + "----WebKitFormBoundarydklhfklsdfhlksh" + "--");
dataOutputStream.flush();
dataOutputStream.close();
urlConnection.connect();
// throw the exception here in case invocation of web service
if (httpURL.getResponseCode() != 200)
{
// throw exception
}
else
{
//SUCCESS
}
}
catch (IOException e)
{
}
EDIT: It throws an error with response code as 500
EDIT: Just to clarify, I'm not trying to upload a file but trying to send the parameters inside the BODY (like Query parameters instead of being sent as URL parameters).
Any pointers or suggestions on this are very much appreciated.
You talk about 'query parameters' and 'parameters in the URL', but neither of the approaches you show does any such things. Both your approaches (try to) send parameters in the request body, aka 'entity', not in the URL. Although body contents may be involved in an application-level query, they are NOT query string aka query parameters at the HTTP level. You also ask 'how do I make this work with HttpURLConnection' as if that were a change or difference when both your attempts already use it.
Your first attempt looks almost correct. It should work if
you .setRequestProperty("Content-type", "application/x-www-form-urlencoded") (which is not automatic) and your values either are URLencoded or don't need it (no reserved characters) (depending on the server it may be enough to have no ampersand or equalsign)
Your second attempt also is fairly close. You need to write a boundary before the first part as well, and for each part after Content-disposition: form-data; name="blah" you need one CRLF to end that header line and a second CRLF to end the header block. (MIME multipart format allows multiple header lines in general, although in this case only one is needed.) And the end boundary should be followed by a CRLF (after the extra --).
Both only if you have the URL correct, of course. Nothing will work without the correct URL.
Best Method to Call WebService with HttpUrlConnection PUT Method
ApiListener apilistener=null;
public void updateWorker()
{
progressDialog = ProgressDialog.show(myContext, "Message",
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
Thread runThread = new Thread(new Runnable() {
#Override
public void run() {
HttpAppRequest http = new HttpAppRequest();
try {
JSONObject paramObject = new JSONObject();
JSONObject dataObject = new JSONObject();
dataObject.put("id", csId);
}
paramObject.put("data", dataObject);
Log.e(AppConstants.TAG, "Param = " + paramObject.toString());
AppResponse response = http.putJSONData(BASE_URL + "/updateapi", paramObject.toString(), true);
if (response.getStatusCode() == 200) {
String csUpdateResult = response.getContentData();
Log.e(AppConstants.TAG, csUpdateResult);
JSONObject updateObject = new JSONObject(csUpdateResult);
Message completeMessage = handler.obtainMessage(1, updateObject);
completeMessage.sendToTarget();
} else {
handler.sendEmptyMessage(-1);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
String csMessage = myContext.getResources().getString(R.string.id_network_response_failure);
Message completeMessage = handler.obtainMessage(0, csMessage);
completeMessage.sendToTarget();
}
}
});
runThread.start();
}
/*******************************************************************************************************/ Handler Api Response Here
Handler handler= new Handler() {
#Override
public void handleMessage(Message inputMessage) {
progressDialog.dismiss();
if (inputMessage.what == 1) {
try {
JSONObject msgObject = (JSONObject) inputMessage.obj;
if (msgObject.has("result")) {
JSONObject resultObject = msgObject.getJSONObject("result");
if (resultObject.has("status")) {
String csStatus = resultObject.getString("status");
if (csStatus.equalsIgnoreCase("success")) {
apilistener.onUpdate(resultObject.getString("msg"));
}
} else {
if(resultObject.has("status"))
{
apilistener.onFailed(resultObject.getString("reason"));
}
}
}
} catch (JSONException e) {
CommonMethods.showMessageBox("", e.getMessage(), myContext);
}
} else if (inputMessage.what == 0) {
String csMessage = (String) inputMessage.obj;
CommonMethods.showMessageBox("", csMessage, myContext);
}
}
};
//CallBack Listener to parent Activity/Fragment
//User listener like this
public interface ApiListener extends EventListener
{
void onSuccess(String msg);
void onFaiulure(String msg);
}
public void setListener(ApiListener listener)
{
apilistener=listener;
}
}
Apologize for the long question!
I am new in SignalR. Please help me to solve my problem.
I have SignalR server in web, I am getting data from .net client site but unable to get data from my Android client side. When my debugger arrived to "hub.invoke" is just stop.
I have tired a lot with different codes but un-success.
If I unable to do above then I want to do that in my local server at my home. I have two laptops, one is running Win7 another Win8. I want to keep a signalR server in one of them and access from another. How can I do that? what kind of SignalR server API I will write? If you guys please help me step my step?
bellow is my tied code
String android_id = Settings.Secure.getString(arg0.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
HubConnection con = new HubConnection(Uri.parse("http://10.198.40.32:8090/login/").toString());
HubProxy hub = con.createHubProxy("NotificationHub");
con.start();
try {
hub.invoke( "userID", "password", android_id).get();
} catch (InterruptedException e) {
// Handle ...
System.out.print(e);
} catch (ExecutionException e) {
// Handle ...
System.out.print(e);
}
You can refer to my working sample code as the following. Then, you can replace Object.class by your own custom class:
public <T> void startSignalR(String transport, String serverUrl, final String userName, final Class<T> tClass) {
Platform.loadPlatformComponent(new AndroidPlatformComponent());
Credentials credentials = new Credentials() {
#Override
public void prepareRequest(Request request) {
request.addHeader(HEADER_KEY_USERNAME, userName);
}
};
mConnection = new HubConnection(serverUrl);
mConnection.setCredentials(credentials);
mHub = mConnection.createHubProxy(SERVER_HUB_CHAT);
if (transport.equals("ServerSentEvents")) {
mTransport = new ServerSentEventsTransport(mConnection.getLogger());
} else if (transport.equals("LongPolling")) {
mTransport = new LongPollingTransport(mConnection.getLogger());
}
mAwaitConnection = mConnection.start(mTransport);
try {
mAwaitConnection.get();
} catch (InterruptedException e) {
e.printStackTrace();
return;
} catch (ExecutionException e) {
e.printStackTrace();
return;
}
mHub.on("broadcastMessage",
new SubscriptionHandler1<Object>() {
#Override
public void run(final Object msg) {
final String finalMsg;
Gson gson = new Gson();
Object object = gson.fromJson(msg.toString(), tClass);
Field[] fields = object.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
System.out.println("Value = " + fields[i].get(object));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
, Object.class);
...
}
A full source code of SignalR for Android/Java, you can find here at GitHub
Hope this helps!
I added in the MainActivity a button click event:
public void addListenerOnButton()
{
btnClick = (Button) findViewById(R.id.checkipbutton);
btnClick.setOnClickListener(new OnClickListener()
{
byte[] response = null;
#Override
public void onClick(View arg0)
{
text = (TextView) findViewById(R.id.textView2);
Thread t = new Thread(new Runnable()
{
#Override
public void run()
{
for (int i = 0; i < ipaddresses.length; i++)
{
try
{
response = Get(ipaddresses[i]);
break;
} catch (Exception e)
{
text.setText("Connection Failed");
}
}
if (response!=null)
{
String a = null;
try
{
a = new String(response,"UTF-8");
text.setText(a);
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
Logger.getLogger("MainActivity(inside thread)").info(a);
}
}
});
t.start();
}
});
}
I wanted to create a break when it's entering the try block after doing the response = Get(ipaddresses[i]); in order to stop the for loop.
The problem is that after it's done the response = Get(ipaddresses[i]); when it's supposed to be doing the break, my program crashes.
On the android device I get the message:
unfortunately myapp has stopped
And when I click ok on the message the program just closes.
I can't figure out why the break makes the program crash.
This is the Get method:
private byte[] Get(String urlIn)
{
URL url = null;
String urlStr = urlIn;
if (urlIn!=null)
urlStr=urlIn;
try
{
url = new URL(urlStr);
} catch (MalformedURLException e)
{
e.printStackTrace();
return null;
}
HttpURLConnection urlConnection = null;
try
{
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
byte[] buf=new byte[10*1024];
int szRead = in.read(buf);
byte[] bufOut;
if (szRead==10*1024)
{
throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. we don't handle it..");
}
else
{
bufOut = Arrays.copyOf(buf, szRead);
}
return bufOut;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
finally
{
if (urlConnection!=null)
urlConnection.disconnect();
}
}
The reason for the crash is most likely apparent from the stacktrace that you haven't shown us.
But the logic of that loop is pretty dubious ... to me.
Without the break, the loop iterates over all of the IP addresses, and tries Get on each one. At the end, response will be the last value returned by a Get call, which may or may not be null.
With the break, the loop terminates after the first IP address for which Get doesn't throw an exception ... irrespective of what the Get call returns. (That could be null.)
These could be the cause of your crash, but it could be something else. Either way, the logic is suspicious. (And calling a method Get is bad style!)
UPDATE
Given that the Get method catches exceptions and returns null on failure, the recommended structure for the code that calls it is:
for (int i = 0; i < ipaddresses.length; i++) {
response = Get(ipaddresses[i]);
if (response != null) {
break;
}
}
if (response == null) {
// notify connection failed
} else {
// process response
}
There is not need for a "belt and braces" try {...} catch in the calling code ... if you have already dealt with the expected exceptions in Get. And (IMO) you should (almost) never catch Exception, because that is liable to conceal bugs.
I have Android Studio with wamp server. I am trying to use a php file to access MySql database on phpmyadmin. I have tried the emulator and Android Device. I am always getting this error :
org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
This is my MainActivity.jav file
public class MainActivity extends ActionBarActivity {
private TextView responseTView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.responseTView = (TextView) findViewById(R.id.response);
new getDetails().execute(new SqlConnector());
}
private void setTextToTextView(JSONArray jsonArray) {
String s = "";
for(int i=0; i<jsonArray.length();i++) {
JSONObject obj = null;
try {
obj = jsonArray.getJSONObject(i);
s = s + "ID: " + obj.getString("id") + "Customer Name:" + obj.getString("Customer");
}
catch(JSONException e) {
e.printStackTrace();
}
}
this.responseTView.setText(s);
}
private class getDetails extends AsyncTask<SqlConnector,Long,JSONArray> {
#Override
protected JSONArray doInBackground(SqlConnector... params) {
return params[0].getDetailsinJson();
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
setTextToTextView(jsonArray);
}
}
}
And this is my SqlConnector.java file
public class SqlConnector {
public JSONArray getDetailsinJson() {
String url = "http://10.0.2.2:8080/D:/Database/main1.php";
HttpEntity httpEntity = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
Log.d("This is inside HTTP try block.","No error..");
HttpResponse httpResponse = httpClient.execute(httpGet); //Using Logs, it's this line that brings on the error. That's my understanding.
httpEntity=httpResponse.getEntity();
}
catch(ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
Log.d("Inside SqlConnector Class", "ajsdhlk");
JSONArray jsonArray = null;
if(httpEntity != null) {
try {
String response= EntityUtils.toString(httpEntity);
Log.e("This is the response", response);
jsonArray = new JSONArray(response);
}
catch(JSONException e) {
Log.d("This is inside Catch block.","Error in JSONArray");
e.printStackTrace();
}
catch(IOException e) {
Log.d("This is inside catch block.","IOException");
e.printStackTrace();
}
}
return jsonArray;
}
}
I have read many posts on Stackoverflow over similar problems. But, I am not able to get past this error after trying many things. I tried changing the IP I have used but it doesn't help. Please help.
I am quite new to Android Studio, so please explain the solutions. Thanks a lot for help.
After trying lot of options for hours. I changed my server. I used an online web hosting site to store my files. And, this somehow stopped this error. If you think there is some other way out, to use wamp then please do let me know.
I managed to fix this problem by writing my IP address from cmd -> ipconfig in the url:
String url = "http://192.168.x.x/D:/Database/main1.php";
I recently started learning android development (am new to java as well) and I am currently working on a chat/messenger application
The problem I am facing, as the title says, is that the listview in which the messages are shown does not update on the device, unless scrolled, but it works fine on the virtual machine. I only tested on LG Optimus l5 II so far, but i need to fix this anyway.
I think it has something to do with multithreading, because this didn't happen until i added some new threads, so the adapter for listview, android manifest and rest I say are set up correctly. I can add them if it helps.
The 2 threads i added that might cause this:
Checks the connection status and if disconnected tries to reconnect.
The thread used for communicating with the server.
I tested running only with the second thread on, and the problem still occurs.
I want to specify this is the first time I try something like this (servers-client, multithreading, java, android (I'm still in college and they don`t teach us these kinds of stuff there) ), and had no documentation ahead regarding how I should set up the communication between the server and the client. This is the most efficient way I could think of.
this is at the end of onCreate:
StartConnectingRoutine(); // so you know where it all starts
and the code for it:
private void StartConnectingRoutine()
{
Thread t = new Thread()
{
#Override
public void run()
{
while(true)
{
if(!connected)
{
if( connect != null)
{
if(!connect.isAlive())
{
ConnectListener();
}
}
else
{
ConnectListener();
}
}
try {
sleep(CONNECTION_CHECK_TIME_MS); // this is set to 10000 (10 seconds)
} catch (InterruptedException e) {
Log.e("Intrerrupted", e.toString());
}
}
}
};
t.start();
}
and the connectListener():
private void ConnectListener()
{
Log.d("Connecting", "Connecting...");
connect = new Thread()
{
JSONObject info = new JSONObject();
String receivedMessage;
#Override
public void run()
{
try {
info.put("Name", user.GetName());
info.put("PORT", MY_PORT);
info.put("IPv4", getIpAddress());
} catch (JSONException e1) {
Log.e("JSON", "JSON error: " + e1.toString());
}
try
{
ServerSocket = new Socket(SERVER_IP, SERVER_PORT);
dis = new DataInputStream(ServerSocket.getInputStream());
dos = new DataOutputStream(ServerSocket.getOutputStream());
dos.writeUTF(info.toString());
dos.flush();
String response = dis.readUTF();
if(response.equals("connected"))
{
Log.d("Connect", "Connected!");
connected = true;
}
else
Log.d("Connect", "Failed to connect!");
while(connected)
{
receivedMessage = dis.readUTF();
DisplayNewMessage(new MMessage(receivedMessage, MMessage.MessageType.Received));
}
}catch(SocketException e)
{
try {
if(connected)
{
ServerSocket.close();
dis.close();
dos.close();
connected = false;
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
catch(Exception e)
{
Log.d("Connect", "Failed to connect");
Log.e("Connect", e.toString());
connected = false;
}
}
};
connect.start();
}
Fixed:
Reconnecting thread (i tried using asyncTask for this too, but it wouldn`t open the other asyncTask, even if I tried to open it from onProgressUpdate()-which it is supposed to be able to run ui thread components):
private void startConnectingRoutine()
{
Thread t = new Thread()
{
#Override
public void run()
{
Log.d("ConnectingRoutine", "Started connecting routine.");
while(true)
{
if(!connected)
{
startListener();
}
try {
sleep(CONNECTION_CHECK_TIME_MS);
} catch (InterruptedException e) {
Log.e("Intrerrupted", e.toString());
}
}
}
};
t.start();
}
Listener thread:
private void startListener()
{
new Listener().execute();
}
.
private class Listener extends AsyncTask<Long, String, Long>
{
#Override
protected Long doInBackground(Long... params) {
Log.d("Connecting...", "Connecting...");
JSONObject info = new JSONObject();
String receivedMessage;
try {
info.put("Name", user.GetName());
info.put("PORT", MY_PORT);
info.put("IPv4", getIpAddress());
} catch (JSONException e1) {
Log.e("JSON", "JSON error: " + e1.toString());
}
try
{
serverSocket = new Socket(SERVER_IP, SERVER_PORT);
dis = new DataInputStream(serverSocket.getInputStream());
dos = new DataOutputStream(serverSocket.getOutputStream());
dos.writeUTF(info.toString());
dos.flush();
String response = dis.readUTF();
if(response.equals("connected"))
{
Log.d("Connect", "Connected!");
connected = true;
}
else
Log.d("Connect", "Failed to connect!");
while(connected)
{
receivedMessage = dis.readUTF();
publishProgress(receivedMessage);
}
}
catch(Exception e)
{
Log.d("Connect", "Failed to connect");
Log.e("Connect", e.toString());
return null;
}
return null;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
displayNewMessage(new MMessage(values[0], MMessage.MessageType.Received));
}
#Override
protected void onPostExecute(Long result) {
super.onPostExecute(result);
connected = false;
try{
if(serverSocket != null)
serverSocket.close();
if(dis != null)
dis.close();
if(dos != null)
dos.close();
}catch(Exception e)
{
Log.e("Listener", "There was a problem closing the connection: " + e.toString());
}
}
}
There are perhaps multiple things going wrong here, but two that jump out are:
You're calling DisplayNewMessage() from outside the UI thread.
You're not notifying the adapter that its dataset has changed.
I urge you to look in to better mechanisms for executing tasks in the background than simply creating a Thread. Using AsyncTasks would be a good start, but you'll need to take special care to handle tasks between configuration changes (such as rotating the device).
Furthermore, your code is very difficult to read as you capitalize your method names. This is against Java code conventions. You will make things easier for yourself by formatting your code neatly (a good IDE helps with that) and learning to follow conventions!