从Android应用程序https发布请求

时间:2014-05-31 09:16:54

标签: android post https

首先:我对https和android开发都很陌生,所以请尽可能清楚地回答。

我想创建一个登录https网站的应用程序,执行一些操作并在关闭应用程序时再次注销。 Atm,我被困在登录部分。无论我尝试了什么,应用程序在执行httpPost时都会崩溃。

这是我从各种博客和Q& A网站获取的内容(以谷歌登录为例):

    String GMAIL_LOGIN = "https://mail.google.com";

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", 
                SSLSocketFactory.getSocketFactory(), 443));

    HttpParams params = new BasicHttpParams();

    SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);

    HttpClient httpClient = new DefaultHttpClient(mgr, params);

    HttpPost httpPost = new HttpPost(GMAIL_LOGIN);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
    nameValuePairs.add(new BasicNameValuePair("Email", username));
    nameValuePairs.add(new BasicNameValuePair("Passwd", password));
    nameValuePairs.add(new BasicNameValuePair("signIn", "Sign In"));

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request 
        HttpResponse response = httpClient.execute(httpPost);
//          
//          if (response.getStatusLine().getStatusCode() < 400) {
//              alert("Login succeeded");
//            }
//            else{
//              alert("Login failed");
//            }
    }
    catch (IOException ioe) {
        alert("IOException caught!");
        //do something about the error
    }

在我迄今为止所做的所有尝试中,当执行httpPost httpClient.execute(httpPost)时,应用程序崩溃了。我做错了什么?

更新:

这是从启动应用程序(在模拟器中)到崩溃,自动重启和关闭应用程序的完整LogCat:

05-31 05:51:37.684: D/dalvikvm(1991): GC_FOR_ALLOC freed 176K, 8% free 2981K/3224K, paused 36ms, total 40ms
05-31 05:51:37.684: I/dalvikvm-heap(1991): Grow heap (frag case) to 3.578MB for 635812-byte allocation
05-31 05:51:37.724: D/dalvikvm(1991): GC_FOR_ALLOC freed 4K, 7% free 3597K/3848K, paused 33ms, total 33ms
05-31 05:51:38.134: I/Choreographer(1991): Skipped 48 frames!  The application may be doing too much work on its main thread.
05-31 05:51:38.204: D/gralloc_goldfish(1991): Emulator without GPU emulation detected.
05-31 05:51:44.304: I/Choreographer(1991): Skipped 116 frames!  The application may be doing too much work on its main thread.
05-31 05:51:46.014: D/dalvikvm(1991): GC_FOR_ALLOC freed 49K, 3% free 4063K/4176K, paused 27ms, total 34ms
05-31 05:51:46.064: I/Choreographer(1991): Skipped 87 frames!  The application may be doing too much work on its main thread.
05-31 05:51:53.204: D/AndroidRuntime(1991): Shutting down VM
05-31 05:51:53.204: W/dalvikvm(1991): threadid=1: thread exiting with uncaught exception (group=0xb2a9aba8)
05-31 05:51:53.224: E/AndroidRuntime(1991): FATAL EXCEPTION: main
05-31 05:51:53.224: E/AndroidRuntime(1991): Process: com.example.protime360_basics, PID: 1991
05-31 05:51:53.224: E/AndroidRuntime(1991): java.lang.IllegalStateException: Could not execute method of the activity
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.view.View$1.onClick(View.java:3823)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.view.View.performClick(View.java:4438)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.view.View$PerformClick.run(View.java:18422)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.os.Handler.handleCallback(Handler.java:733)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.os.Looper.loop(Looper.java:136)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at java.lang.reflect.Method.invoke(Method.java:515)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at dalvik.system.NativeStart.main(Native Method)
05-31 05:51:53.224: E/AndroidRuntime(1991): Caused by: java.lang.reflect.InvocationTargetException
05-31 05:51:53.224: E/AndroidRuntime(1991):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at java.lang.reflect.Method.invoke(Method.java:515)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.view.View$1.onClick(View.java:3818)
05-31 05:51:53.224: E/AndroidRuntime(1991):     ... 11 more
05-31 05:51:53.224: E/AndroidRuntime(1991): Caused by: android.os.NetworkOnMainThreadException
05-31 05:51:53.224: E/AndroidRuntime(1991):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-31 05:51:53.224: E/AndroidRuntime(1991):     at com.example.protime360_basics.LoginActivity.submitLogin(LoginActivity.java:152)
05-31 05:51:53.224: E/AndroidRuntime(1991):     ... 14 more
05-31 05:51:56.544: D/dalvikvm(2019): GC_FOR_ALLOC freed 183K, 8% free 2983K/3232K, paused 33ms, total 36ms
05-31 05:51:56.544: I/dalvikvm-heap(2019): Grow heap (frag case) to 3.581MB for 635812-byte allocation
05-31 05:51:56.584: D/dalvikvm(2019): GC_FOR_ALLOC freed 4K, 7% free 3600K/3856K, paused 33ms, total 33ms
05-31 05:51:57.024: I/Choreographer(2019): Skipped 60 frames!  The application may be doing too much work on its main thread.
05-31 05:51:57.044: D/gralloc_goldfish(2019): Emulator without GPU emulation detected.
05-31 05:51:59.364: I/Choreographer(2019): Skipped 102 frames!  The application may be doing too much work on its main thread.`

1 个答案:

答案 0 :(得分:1)

您应该在单独的线程上执行网络任务。这可以通过扩展AsyncTask

来完成
private class MyNetworkTask extends AsyncTask<Params, Progress, Result>{
protected JSONArray doInBackground(Params... params) {
        //Your network code here. 
    }
}

然后,您可以创建此类的实例,并使用您指定类型的参数调用execute()方法作为'&lt;&gt;'之间的第一个Object类型类标题中的括号。

查看documentation