执行http请求Android时出现Null异常

时间:2012-08-02 17:56:06

标签: android http hosting

我有一个null异常执行这个代码,在“执行”操作,我不明白为什么,你能不能发牢骚我? 在这里,我的代码撤回了一个地址:“routeslibre.fr/connexion.php?variable = Departement ;;; voie ;;;”并将其提供给http操作:执行。在这个操作上抛出一个异常,它有一个空值,我不明白原因....?谢谢你的帮助!

    mydataFromHMI = dataFromHMI;
    myFichierPHPbase = FichierPHPbase;

    String result = null;
    InputStream is = null;
    JSONObject json_data=null;

    try{
        GestionStrings gestionString = new GestionStrings();
        splitData = mydataFromHMI.split(";;;");
        mydataFromHMI = gestionString.blancTraitement(splitData[0]) 
                + ";;;" 
                + gestionString.blancTraitement(splitData[1]) 
                + ";;;";
        String httpRequest = new String("http://routeslibre.fr/"+ myFichierPHPbase + "?variable=" + mydataFromHMI);
        HttpGet httpGet = new HttpGet(httpRequest);
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used. 
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpResponse response = httpclient.execute(httpGet);

        //If you get the value of response code as 200 then you are getting data from server and 
        //if the response code value >=300 then you have error at your server side.
        //int test = response.getStatusLine().getStatusCode();

        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }
    catch(Exception e){
        Log.i("taghttppost",""+e.toString());
        isProblemConnexion = true;
    }

日志是:

  `  08-02 19:58:57.959: I/dalvikvm-heap(7640): Grow heap (frag case) to 15.421MB for 1647516-byte allocation
08-02 19:58:58.089: I/webclipboard(7640): clipservice: android.sec.clipboard.ClipboardExManager@419ff9b8
08-02 19:58:58.164: I/webclipboard(7640): clipservice: android.sec.clipboard.ClipboardExManager@419ff9b8
08-02 19:58:58.174: E/SKIA(7640): FimgApiStretch:stretch failed
08-02 19:58:58.324: I/webclipboard(7640): clipservice: android.sec.clipboard.ClipboardExManager@419ff9b8
08-02 19:58:58.364: I/Ads(7640): To get test ads on this device, call adRequest.addTestDevice("1CA948FD73C51AEDDBB30790A88E0DBC");
08-02 19:58:58.514: I/Ads(7640): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"kw":["voiture moto velo bus trameway tram train greve carte geolocalisation"],"preqs":0,"session_id":"10528273920811926641","u_sd":2,"seq_num":"1","slotname":"a15007f67ec6dea","u_w":360,"msid":"free.controls.source","js":"afma-sdk-a-v6.0.1","mv":"8013015.com.android.vending","isu":"1CA948FD73C51AEDDBB30790A88E0DBC","cipa":1,"format":"320x50_mb","net":"ed","app_name":"4.android.free.controls.source","hl":"fr","u_h":640,"carrier":"20820","ptime":0,"u_audio":1});</script></head><body></body></html>
08-02 19:59:02.174: I/taghttppost(7640): org.apache.http.client.ClientProtocolException
08-02 19:59:02.174: I/tagconvertstr(7640): java.lang.NullPointerException
08-02 19:59:02.179: I/connexions(7640): java.util.concurrent.ExecutionException: java.lang.NullPointerException
08-02 19:59:02.179: W/dalvikvm(7640): threadid=19: thread exiting with uncaught exception (group=0x40c661f8)
08-02 19:59:02.194: E/AndroidRuntime(7640): FATAL EXCEPTION: AsyncTask #2
08-02 19:59:02.194: E/AndroidRuntime(7640): java.lang.RuntimeException: An error occured while executing doInBackground()
08-02 19:59:02.194: E/AndroidRuntime(7640):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.lang.Thread.run(Thread.java:856)
08-02 19:59:02.194: E/AndroidRuntime(7640): Caused by: java.lang.NullPointerException
08-02 19:59:02.194: E/AndroidRuntime(7640):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at org.json.JSONArray.<init>(JSONArray.java:87)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at org.json.JSONArray.<init>(JSONArray.java:103)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at free.controls.source.clien.ConnexionSQL.ConnexionBD(ConnexionSQL.java:213)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at free.controls.source.clien.ConnexionSQL.doInBackground(ConnexionSQL.java:76)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at free.controls.source.clien.ConnexionSQL.doInBackground(ConnexionSQL.java:1)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-02 19:59:02.194: E/AndroidRuntime(7640):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-02 19:59:02.194: E/AndroidRuntime(7640):     ... 5 more
08-02 19:59:02.339: E/WindowManager(7640): Activity free.controls.source.forum.ForumActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bf0540 that was originally added here
08-02 19:59:02.339: E/WindowManager(7640): android.view.WindowLeaked: Activity free.controls.source.forum.ForumActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bf0540 that was originally added here
08-02 19:59:02.339: E/WindowManager(7640):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:414)
08-02 19:59:02.339: E/WindowManager(7640):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:282)
08-02 19:59:02.339: E/WindowManager(7640):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:218)
08-02 19:59:02.339: E/WindowManager(7640):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:143)
08-02 19:59:02.339: E/WindowManager(7640):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.Dialog.show(Dialog.java:278)
08-02 19:59:02.339: E/WindowManager(7640):  at free.controls.source.clien.ConnexionSQL.onPreExecute(ConnexionSQL.java:84)
08-02 19:59:02.339: E/WindowManager(7640):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
08-02 19:59:02.339: E/WindowManager(7640):  at android.os.AsyncTask.execute(AsyncTask.java:511)
08-02 19:59:02.339: E/WindowManager(7640):  at free.controls.source.forum.ForumActivity.onCreate(ForumActivity.java:78)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.Activity.performCreate(Activity.java:4533)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.ActivityThread.access$600(ActivityThread.java:128)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-02 19:59:02.339: E/WindowManager(7640):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 19:59:02.339: E/WindowManager(7640):  at android.os.Looper.loop(Looper.java:137)
08-02 19:59:02.339: E/WindowManager(7640):  at android.app.ActivityThread.main(ActivityThread.java:4517)
08-02 19:59:02.339: E/WindowManager(7640):  at java.lang.reflect.Method.invokeNative(Native Method)
08-02 19:59:02.339: E/WindowManager(7640):  at java.lang.reflect.Method.invoke(Method.java:511)
08-02 19:59:02.339: E/WindowManager(7640):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-02 19:59:02.339: E/WindowManager(7640):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-02 19:59:02.339: E/WindowManager(7640):  at dalvik.system.NativeStart.main(Native Method)
08-02 19:59:03.464: I/GATE(7640): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>`

2 个答案:

答案 0 :(得分:0)

在213行检查你的java文件ConnexionSQL.java,从这一行生成nullpointer异常。

答案 1 :(得分:0)

来自这一行的错误:

HttpGet httpGet = new HttpGet(httpRequest);

我改变了这个:

HttpPost request = new HttpPost(httpRequest);