HTTP连接

时间:2013-04-12 22:16:15

标签: android httpwebrequest

我正在尝试使用我的登录名和密码编写示例http连接。

这是我的代码:

        String urlModify ="http://app-vantage.appspot.com/api4/modified";
        String username = java.util.UUID.randomUUID().toString();
        Log.e(getClass().getName(), "username = "+username);
        String key ="FU2ra88xuhuf6-#At+aseQub8f8ebr$sweh$thu!Ep?*frusAvEdRamuw9thubr";
        String password = "";
        try {
            password = AeSimpleSHA1.SHA1(username + key);
            Log.e(getClass().getName(), "password = "+password);
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            URL url = new URL(urlModify);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Authentication", "Basic " + android.util.Base64.encodeToString((username + ":" + password).getBytes(), android.util.Base64.DEFAULT));

            urlConnection.connect();
            Log.e(getClass().getName(), "content ="+urlConnection.getContent().toString());
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            try {
                String stream = readStream(in);
                Log.e(getClass().getName(), "stream = "+stream);
            }finally {

                in.close();

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我得到一个例外:

04-12 23:04:28.588: W/System.err(21253): java.io.FileNotFoundException: http://app-vantage.appspot.com/api4/modified
04-12 23:04:28.618: W/System.err(21253):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
04-12 23:04:28.618: W/System.err(21253):    at java.net.URLConnection$DefaultContentHandler.getContent(URLConnection.java:1018)
04-12 23:04:28.618: W/System.err(21253):    at java.net.URLConnection.getContent(URLConnection.java:199)
04-12 23:04:28.623: W/System.err(21253):    at com.sectov.connection.sdk.WebServicesManager.checkLastContentModification(WebServicesManager.java:105)
04-12 23:04:28.623: W/System.err(21253):    at com.sectov.connection.fragments.ConnectionSectionFragment.onActivityCreated(ConnectionSectionFragment.java:49)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
04-12 23:04:28.623: W/System.err(21253):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
04-12 23:04:28.623: W/System.err(21253):    at android.view.View.measure(View.java:15562)
04-12 23:04:28.623: W/System.err(21253):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
04-12 23:04:28.623: W/System.err(21253):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-12 23:04:28.623: W/System.err(21253):    at android.view.View.measure(View.java:15562)
04-12 23:04:28.623: W/System.err(21253):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
04-12 23:04:28.623: W/System.err(21253):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-12 23:04:28.623: W/System.err(21253):    at android.view.View.measure(View.java:15562)
04-12 23:04:28.623: W/System.err(21253):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
04-12 23:04:28.628: W/System.err(21253):    at android.view.View.measure(View.java:15562)
04-12 23:04:28.628: W/System.err(21253):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-12 23:04:28.628: W/System.err(21253):    at android.view.View.measure(View.java:15562)
04-12 23:04:28.628: W/System.err(21253):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-12 23:04:28.628: W/System.err(21253):    at android.view.View.measure(View.java:15562)
04-12 23:04:28.628: W/System.err(21253):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
04-12 23:04:28.628: W/System.err(21253):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-12 23:04:28.628: W/System.err(21253):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
04-12 23:04:28.628: W/System.err(21253):    at android.view.View.measure(View.java:15562)
04-12 23:04:28.628: W/System.err(21253):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1987)
04-12 23:04:28.628: W/System.err(21253):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
04-12 23:04:28.633: W/System.err(21253):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1401)
04-12 23:04:28.633: W/System.err(21253):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1121)
04-12 23:04:28.633: W/System.err(21253):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4598)
04-12 23:04:28.633: W/System.err(21253):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-12 23:04:28.633: W/System.err(21253):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-12 23:04:28.633: W/System.err(21253):    at android.view.Choreographer.doFrame(Choreographer.java:525)
04-12 23:04:28.633: W/System.err(21253):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-12 23:04:28.638: W/System.err(21253):    at android.os.Handler.handleCallback(Handler.java:615)
04-12 23:04:28.638: W/System.err(21253):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-12 23:04:28.638: W/System.err(21253):    at android.os.Looper.loop(Looper.java:137)
04-12 23:04:28.638: W/System.err(21253):    at android.app.ActivityThread.main(ActivityThread.java:4921)
04-12 23:04:28.638: W/System.err(21253):    at java.lang.reflect.Method.invokeNative(Native Method)
04-12 23:04:28.638: W/System.err(21253):    at java.lang.reflect.Method.invoke(Method.java:511)
04-12 23:04:28.638: W/System.err(21253):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
04-12 23:04:28.638: W/System.err(21253):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
04-12 23:04:28.638: W/System.err(21253):    at dalvik.system.NativeStart.main(Native Method)
04-12 23:04:57.253: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:57.253: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:57.253: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:57.258: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:57.258: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:57.258: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:57.258: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:57.258: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:57.263: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:57.263: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:57.263: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): beginBatchEdit on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): endBatchEdit on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:57.268: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:57.273: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:57.278: W/IInputConnectionWrapper(21253): beginBatchEdit on inactive InputConnection
04-12 23:04:57.278: W/IInputConnectionWrapper(21253): endBatchEdit on inactive InputConnection
04-12 23:04:58.653: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.653: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.658: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.658: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.658: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.658: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.783: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): beginBatchEdit on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): endBatchEdit on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): beginBatchEdit on inactive InputConnection
04-12 23:04:58.788: W/IInputConnectionWrapper(21253): endBatchEdit on inactive InputConnection
04-12 23:04:58.818: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.818: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.818: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.818: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.823: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.823: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.823: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.828: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.828: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.828: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.828: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.828: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): beginBatchEdit on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): endBatchEdit on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): getExtractedText on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): getTextBeforeCursor on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): getSelectedText on inactive InputConnection
04-12 23:04:58.833: W/IInputConnectionWrapper(21253): getTextAfterCursor on inactive InputConnection
04-12 23:04:58.838: W/IInputConnectionWrapper(21253): beginBatchEdit on inactive InputConnection
04-12 23:04:58.838: W/IInputConnectionWrapper(21253): endBatchEdit on inactive InputConnection

我无法理解这里有什么问题? 欢迎任何帮助。非常感谢。

1 个答案:

答案 0 :(得分:0)

好像是权限问题.. 您是否在“清单”中设置了应用程序权限以使用互联网?

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

并使用互联网

<uses-permission android:name="android.permission.INTERNET" />