Android HTTP发布和请求数据PHP

时间:2015-03-19 20:46:41

标签: java php android http

我对这种类型的编程很陌生,我从PHP切换到了java应用程序。

我启动了这个应用程序,用于扫描QR码并获取信息。它工作得很好。如果我扫描QR码,我会得到这些信息,我可以在显示屏上进行Toast。

但是,我还需要将这个字符串从QR码作为POST发送到我执行的php页面"魔术"并将值返回给我的应用程序。

我在谷歌搜索了很多,我发现了这部分代码:

 if (scanningResult != null) {
             String scanContent = scanningResult.getContents();



// Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.domain.cz/app.php");

//This is the data to send
            String MyName = scanContent; //any data to send

            try {
// Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                    nameValuePairs.add(new BasicNameValuePair("action", MyName));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request

                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String response = httpclient.execute(httppost, responseHandler);

//This is the response from a php application
                    String reverseString = response;
                    Toast.makeText(getApplicationContext(), "response" + reverseString, Toast.LENGTH_LONG).show();

            } catch (ClientProtocolException e) {
                Toast.makeText(getApplicationContext(), "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
            }
    }

一切都很完美,我可以编译这个应用程序,运行QR代码扫描程序并扫描代码。但我的应用程序失败并在此代码中的某处崩溃。

你能找到我的错误吗?我可以附加logcat日志,但我对此Logging功能感到有些困惑。这里出现了数千行代码。

logcat的

    03-20 06:42:10.945  16134-16134/com.example.sunny.kpcombarcode I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
03-20 06:42:10.975  16134-16134/com.example.sunny.kpcombarcode I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-I9301I_4.4.2_0040
03-20 06:42:10.975  16134-16134/com.example.sunny.kpcombarcode I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
03-20 06:42:10.985  16134-16134/com.example.sunny.kpcombarcode D/dalvikvm﹕ Late-enabling CheckJNI
03-20 06:42:11.055  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ Sending WAIT chunk
03-20 06:42:11.055  16134-16134/com.example.sunny.kpcombarcode W/ActivityThread﹕ Application com.example.sunny.kpcombarcode is waiting for the debugger on port 8100...
03-20 06:42:11.165  16134-16140/com.example.sunny.kpcombarcode I/dalvikvm﹕ Debugger is active
03-20 06:42:11.255  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ Debugger has connected
03-20 06:42:11.255  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:11.455  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:11.655  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:11.855  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:12.055  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:12.255  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:12.465  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:12.665  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:12.865  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ waiting for debugger to settle...
03-20 06:42:13.065  16134-16134/com.example.sunny.kpcombarcode I/System.out﹕ debugger has settled (1396)
03-20 06:42:13.175  16134-16134/com.example.sunny.kpcombarcode E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
03-20 06:42:13.335  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ Get MotionRecognitionManager
03-20 06:42:13.415  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
03-20 06:42:13.415  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:13.435  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
03-20 06:42:13.435  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:13.515  16134-16134/com.example.sunny.kpcombarcode I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
    OpenGL ES Shader Compiler Version: E031.24.00.07
    Build Date: 01/27/14 Mon
    Local Branch: base_au149_adreno_au169_patches
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
03-20 06:42:13.565  16134-16134/com.example.sunny.kpcombarcode D/OpenGLRenderer﹕ Enabling debug mode 0
03-20 06:42:13.615  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:13.615  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
03-20 06:42:13.615  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:13.765  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:13.805  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:23.005  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:23.285  16134-16134/com.example.sunny.kpcombarcode E/MediaPlayer-JNI﹕ QCMediaPlayer mediaplayer NOT present
03-20 06:42:23.305  16134-16256/com.example.sunny.kpcombarcode W/MediaPlayer﹕ info/warning (973, 0)
03-20 06:42:23.335  16134-16134/com.example.sunny.kpcombarcode I/DecodeHintManager﹕ Hints from the Intent: {}
03-20 06:42:23.395  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called
03-20 06:42:23.475  16134-16134/com.example.sunny.kpcombarcode I/com.google.zxing.client.android.camera.open.OpenCameraInterface﹕ Opening camera #0
03-20 06:42:24.195  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Screen resolution: Point(1280, 720)
03-20 06:42:24.195  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Supported preview sizes: 1920x1080 1440x1080 1280x720 1056x864 960x720 800x480 720x480 640x480 352x288 320x240 176x144
03-20 06:42:24.195  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Found preview size exactly matching screen size: Point(1280, 720)
03-20 06:42:24.195  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Camera resolution: Point(1280, 720)
03-20 06:42:24.265  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Initial camera parameters: focallength-value-denominator=100;brightness-step=1;zoom=0;redeye-reduction-values=enable,disable;max-num-detected-faces-hw=2;scene-detect-values=off,on;qc-camera-features=30;face-detection-values=off,on;whitebalance=auto;max-sharpness=36;focallength-value-numerator=371;preview-format-values=yuv420sp,yuv420p;jpeg-thumbnail-quality=85;preview-format=yuv420sp;face-detection=off;auto-exposure-values=matrix,center,spot;iso=auto;fast-fps-mode=0;mce-values=enable,disable;flash-mode-values=off,auto,on,torch;preview-frame-rate=30;jpeg-thumbnail-width=512;video-size=1920x1080;scene-mode-values=auto,asd,landscape,snow,beach,sunset,night,portrait,backlight,sports,steadyphoto,flowers,candlelight,fireworks,party,night-portrait,theatre,action,AR;redeye-reduction=disable;preview-fps-range-values=(7500,119900);histogram=disable;face-recognition=off;camera-mode=0;preview-size-values=1920x1080,1440x1080,1280x720,1056x864,960x720,800x480,720x480,640x480,352x288,320x240,176x144;maxaperture-value-denominator=100;saturation-step=1;touch-af-aec=touch-off;smart-screen-exposure-values=vision-ae-normal,vision-ac-backlight;preview-fps-range=7500,119900;auto-whitebalance-lock=false;min-exposure-compensation=-12;llv_mode=0;antibanding=off;max-num-focus-areas=1;face-recognition-values=off,on;vertical-view-angle=42.5;video-stabilization-supported=false;sce-factor-step=10;maxaperture-value-numerator=253;qc-max-num-requested-faces=2;min-saturation=0;max-saturation=10;contrast-step=1;raw-size=3280x2464;max-brightness=6;supported-live-snapshot-sizes=3264x2448,3264x1836,3200x2400,2592x1944,2048x1536,2048x1152,1920x1080,1600x1200,1440x1080,1280x768,1280x720,1024x768,800x600,800x480,800x450,720x480,640x480,352x288,320x240,176x144;max-contrast=10;min-sce-factor=-100;picture-format-values=jpeg,bayer-qcom-10gbrg,bayer-qcom-10grbg,bayer-qcom-10rggb,bayer-qcom-10bggr,bayer-mipi-10gbrg,bayer-mipi-10grbg,bayer-mipi-10rggb,bayer-mipi-10bggr,yuv422sp,bayer-ideal-qcom-10grbg;video-hfr=off;supported-shot-modes=normal-shot,panorama-burst;exposure-compensation-step=0.166667;scene-detect=off;fnumber-value-numerator=240;picture-size=3264x2448;saturation=5;whitebalance-values=auto,incandescent,fluorescent,warm-fluorescent,daylight,cloudy-daylight,twilight,shade;picture-format=jpeg;zsl=off;lensshade-values=enable,disable;selectable-zone-af=auto;video-hfr-values=90;iso-values=auto,ISO_HJR,100,200,400,800,1600;selectable-zone-af-values=;lensshade=enable;preferred-preview-size-for-video=1920x1080;anti-shake=0;min-sharpness=0;intelligent-mode=0;mce=enable;hfr-size-values=800x450;snapshot-picture-flip=off;zoom-supported=true;metering=center;denoise-values=denoise-off,denoise-on;zsl-values=off,on;sharpness=36;contrast=5;scene-mode=auto;jpeg-quality=85;histogram-values=enable,disable;metering-areas=(0,0,0,0,0);min-contrast=0;video-size-values=1920x1080,1440x1080,1280x720,800x450,720x480,640x480,352x288,320x240,176x144;skinToneEnhancement=0;preview-size=1920x1080;focal-length=3.71;ae-bracket-hdr-values=Off,AE-Bracket;denoise=denoise-on;video-flip=off;max-sce-factor=100;preview-frame-rate-values=15,30;max-num-metering-areas=10;preview-flip=off;focus-mode-values=auto,infinity,macro,continuous-video,continuous-picture;vtmode=0;jpeg-thumbnail-size-values=512x288,480x288,256x154,432x288,512x384,352x288,320x240,176x144,160x160,0x0;zoom-ratios=100,104,109,114,119,125,129,134,139,144,150,154,159,164,169,175,179,184,189,194,200,204,209,214,219,225,229,234,239,244,250,254,259,264,269,275,279,284,289,294,300,304,309,314,319,325,329,334,339,344,350,354,359,364,369,375,379,384,389,394,400,404,409,414;dis-values=enable,disable;dual_mode=0;picture-size-values=3264x2448,3264x1836,2560x1920,2560x1440,2048x1536,2048x1152,1920x1080,1600x1200,1280x960,1280x720,640x480;touch-af-aec-values=touch-off,touch-on;smart-screen-exposure=0;luma-adaptation=3;min-brightness=0;horizontal-view-angle=54.8;firmware-mode=none;flip-mode-values=off,flip-v,flip-h,flip-vh;skinToneEnhancement-values=enable,disable;jpeg-thumbnail-height=288;smooth-zoom-supp
03-20 06:42:24.265  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Requesting flash mode value from among: [off]
03-20 06:42:24.265  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Supported flash mode values: [off, auto, on, torch]
03-20 06:42:24.265  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Can set flash mode to: off
03-20 06:42:24.265  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Flash mode already set to off
03-20 06:42:24.275  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Requesting focus mode value from among: [auto]
03-20 06:42:24.275  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Supported focus mode values: [auto, infinity, macro, continuous-video, continuous-picture]
03-20 06:42:24.275  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Can set focus mode to: auto
03-20 06:42:24.275  16134-16134/com.example.sunny.kpcombarcode I/CameraConfiguration﹕ Focus mode already set to auto
03-20 06:42:24.315  16134-16134/com.example.sunny.kpcombarcode I/DecodeThread﹕ Hints: {POSSIBLE_FORMATS=[QR_CODE], NEED_RESULT_POINT_CALLBACK=com.google.zxing.client.android.ViewfinderResultPointCallback@423bcf78}
03-20 06:42:24.765  16134-16134/com.example.sunny.kpcombarcode I/AutoFocusManager﹕ Current focus mode 'auto'; use auto focus? true
03-20 06:42:24.785  16134-16134/com.example.sunny.kpcombarcode I/MediaPlayer﹕ Don't send intent. msg.arg1 = 0, msg.arg2 = 0
03-20 06:42:24.785  16134-16134/com.example.sunny.kpcombarcode E/MediaPlayer﹕ Should have subtitle controller already set
03-20 06:42:24.785  16134-16134/com.example.sunny.kpcombarcode I/Choreographer﹕ Skipped 80 frames!  The application may be doing too much work on its main thread.
03-20 06:42:24.805  16134-16134/com.example.sunny.kpcombarcode D/CameraManager﹕ Calculated framing rect: Rect(240, 135 - 1040, 585)
03-20 06:42:25.355  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
03-20 06:42:25.355  16134-16134/com.example.sunny.kpcombarcode D/AbsListView﹕ unregisterIRListener() is called

解决:

添加

if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

在setContent下的主要活动中..我认为问题出在这个帖子中的太多工作中。但这个小片段解决了它

0 个答案:

没有答案
相关问题