Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - HttpClient execute has a mind of its own - 1 second or 200 seconds
    primarykey
    data
    text
    <p>I'm trying to use HttpClient to connect to a php page that logs in and passes back a sessionid and then goes to a new page, using that sessionid and obtains a mySQL datafield associated with that sessionid.</p> <p>On the first request, HttpClient can take 1.5 seconds, 6 seconds, or 2 minutes. If the first request was slow, subsequence requests seem to be faster, and visaversa.</p> <p>The HttpClient request occurs when a Button view is clicked</p> <p>Here's my code:</p> <pre><code>public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); name = (TextView)findViewById(R.id.name); user = (EditText) findViewById(R.id.user); pass = (EditText) findViewById(R.id.pass); submit = (Button) findViewById(R.id.button1); submit.setOnClickListener(this); HttpParams params1 = new BasicHttpParams(); params1.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); client = new DefaultHttpClient(params1); httpclient = new DefaultHttpClient(); // Create a local instance of cookie store cookieStore = new BasicCookieStore(); // Create local HTTP context } public void onClick(View v) { if (v.getId() == R.id.button1) { //submit.setClickable(false); String username = user.getText().toString(); String password = pass.getText().toString(); String targetURL = "&lt;&lt;&lt;&lt;LOGIN PHP URL&gt;&gt;&gt;&gt;"; post = new HttpPost(targetURL); List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); params.add(new BasicNameValuePair("username", username)); params.add(new BasicNameValuePair("password", password)); Log.d("params","params added"); try { post.setEntity(new UrlEncodedFormEntity(params)); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Log.d("entity added","entityadded"); Log.d("preex","PRE EXECUTION"); localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); //submit.setText("Logging In..."); new Thread(new Runnable(){ public void run(){ try { Log.d("pre","pre execute"); response = client.execute(post,localContext); Log.d("post","post execute"); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Log.d("post","FIANLLY"); try { input = response.getEntity().getContent(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.d("response: ",convertStreamToString(input)); getFullName(localContext); } } }).start();} } private void getFullName(final HttpContext context){ Log.d("called","called"); String targetURL = "&lt;&lt;&lt;&lt;SESSION CHECKER PHP URL&gt;&gt;&gt;&gt;"; //client1 = new DefaultHttpClient(); post1 = new HttpPost(targetURL); Log.d("","about to call runable...."); // submit.setText("Obtaining Full Name..."); try { Log.d("pre","CALLING!"); response = client.execute(post1,context); Log.d("post","called.."); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Log.d("post","FIANLLY"); try { //submit.setText("Full Name Obtained!..."); input = response.getEntity().getContent(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Log.d("response: ",convertStreamToString(input)); outputResponse(input); } } private void outputResponse(final InputStream in) { name.post(new Runnable(){ public void run(){ String fullname=convertStreamToString(in); name.setText("Full Name is: "+fullname); } }); } private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 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(); } </code></pre> <p>Before I set Http version 1.1 it took double the time, but for my application, the speed cannot be unreliable. This is all on a pretty fast WiFi connections -- can you image Edge or 3G speeds??</p> <p>So what can I optimize?</p> <p>Thanks everyone:)</p> <hr> <p>EDIT: I did a new test with: <a href="http://www.posttestserver.com/" rel="nofollow">http://www.posttestserver.com/</a> and it happened pretty fast. The urls I'm using currently aren't my final server urls, they are for a different site on a different server -- shared hosting, so could it be that contacting my shared hosting site is just slow and will be compared to my .net server? </p> <p>Thanks again !</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload