开发者

Android - HttpClient execute has a mind of its own - 1 second or 200 seconds

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.

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.

The HttpClient request occurs when a Button view is clicked

Here's my 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 = "<<<<LOGIN PHP URL>>>>";

        post = new HttpPost(targetURL);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        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")开发者_如何学Python;

        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 = "<<<<SESSION CHECKER PHP URL>>>>";
    //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();
}

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??

So what can I optimize?

Thanks everyone:)


EDIT: I did a new test with: http://www.posttestserver.com/ 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?

Thanks again !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜