一个数组的Android NullPointer

时间:2014-12-23 04:09:53

标签: java android json rest

我有一个在浏览器上返回的Web服务:

<ArrayOfclients xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebMon.Models">
<clients>
<ClientID>1</ClientID>
<Name>001</Name>
<Telephone i:nil="true"/>
<accumulated>0</accumulated>
<dependant i:nil="true"/>
<description i:nil="true"/>
<id_route_cuadre>1</id_route_cuadre>
<lastName i:nil="true"/>
<status>1</status>
</clients>
<clients>
<ClientID>2</ClientID>
<Name>002</Name>
<Telephone i:nil="true"/>
<accumulated>0</accumulated>
<dependant i:nil="true"/>
<description i:nil="true"/>
<id_route_cuadre>2</id_route_cuadre>
<lastName i:nil="true"/>
<status>1</status>
</clients>
<clients>
<ClientID>3</ClientID>
<Name>003</Name>
<Telephone i:nil="true"/>
<accumulated>0</accumulated>
<dependant i:nil="true"/>
<description i:nil="true"/>
<id_route_cuadre>3</id_route_cuadre>
<lastName i:nil="true"/>
<status>1</status>
</clients>
<clients>
<ClientID>4</ClientID>
<Name>004</Name>
<Telephone i:nil="true"/>
<accumulated>0</accumulated>
<dependant i:nil="true"/>
<description i:nil="true"/>
<id_route_cuadre>4</id_route_cuadre>
<lastName i:nil="true"/>
<status>1</status>
</clients>
<clients>
<ClientID>5</ClientID>
<Name>005</Name>
<Telephone i:nil="true"/>
<accumulated>0</accumulated>
<dependant i:nil="true"/>
<description i:nil="true"/>
<id_route_cuadre>5</id_route_cuadre>
<lastName i:nil="true"/>
<status>1</status>
</clients>
</ArrayOfclients>

但是当我尝试在Android上进行请愿时,请给我一个nullPointer,即使请愿书在网址上也是如此。

android的代码:

HttpClient httpclient = new DefaultHttpClient();


    ws = (String) params[0].get(0);
    HttpResponse response = null;
    webparams = (List<NameValuePair>) params[0].get(1);
    HttpGet httppost;
    String connURL="";
    if (ws.indexOf(":")>0){
        connURL +=new ObjConexion().Url()+"/api/"+ws.split(":")[0];
    }else{
        connURL +=new ObjConexion().Url()+"/api/"+ws;
    }

    for (int cont = 0; cont<webparams.size(); cont++){
        if(cont>=1){
            connURL += "&";
        }
        if(cont==0){
            connURL +="?";
        }
        connURL+=webparams.get(cont).getName()+"="+webparams.get(cont).getValue();
    }

    httppost = new HttpGet(connURL);
    try {
        // Add your data

        // Execute HTTP Post Request
        response = httpclient.execute(httppost);
        if(response == null){
            Log.wtf("Mistake:", "El valor es null");
        }


        try {
            cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
        } catch (Exception e) {
        }
        return response;


    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Log.e("Error:", e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("Error:", e.getMessage());
    }
    return response; 

我的堆栈跟踪日志:

12-22 23:57:00.464: I/System.out(6424): httpget:http://x.x.x.x:x/api/clients?id=43
12-22 23:57:00.465: I/System.out(6424): http://x.x.x.x:x/api/clients?id=43
12-22 23:57:00.467: V/PhoneWindow(6424): DecorView setVisiblity: visibility = 4
12-22 23:57:00.472: I/System.out(6424): [socket][1] connection /209.126.71.80:8091;LocalPort=49434(0)
12-22 23:57:00.473: I/System.out(6424): [CDS]connect[/209.126.71.80:8091] tm:90
12-22 23:57:00.474: D/Posix(6424): [Posix_connect Debug]Process charge.system :8091 
12-22 23:57:00.474: V/PhoneWindow(6424): DecorView setVisiblity: visibility = 0
12-22 23:57:00.476: V/InputMethodManager(6424): focusOut: android.widget.EditText{41306088 VFED..CL .F...... 50,149-268,189 #7f08000d app:id/username} mServedView=android.widget.EditText{41306088 VFED..CL .F...... 50,149-268,189 #7f08000d app:id/username} winFocus=false
12-22 23:57:00.491: V/InputMethodManager(6424): Not IME target window, ignoring
12-22 23:57:00.515: I/(6424): [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
12-22 23:57:00.515: I/(6424): [Mali]max_allowed_dequeued_buffers=3
12-22 23:57:00.526: I/SurfaceTextureClient(6424): [STC::queueBuffer] (this:0x51934918) fps:2.16, dur:1388.43, max:521.31, min:398.98
12-22 23:57:00.553: V/InputMethodManager(6424): onWindowFocus: com.android.internal.policy.impl.PhoneWindow$DecorView{412f6118 VFE..... RF....I. 0,0-320,359} softInputMode=272 first=true flags=#1810100
12-22 23:57:00.553: D/InputMethodManager(6424): deactivate the inputconnection in ControlledInputConnectionWrapper.
12-22 23:57:00.553: V/InputMethodManager(6424): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{412f6118 VFE..... RF....I. 0,0-320,359} ic=null tba=android.view.inputmethod.EditorInfo@412ef2c0 controlFlags=#105
12-22 23:57:00.589: V/InputMethodManager(6424): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@412ef430 com.android.inputmethod.latin/.LatinIME #117}
12-22 23:57:00.595: I/System.out(6424): [socket][/192.168.0.5:49434] connected
12-22 23:57:00.595: I/System.out(6424): [CDS]rx timeout:0
12-22 23:57:00.597: I/System.out(6424): >doSendRequest
12-22 23:57:00.598: I/System.out(6424): <doSendRequest
12-22 23:57:00.650: D/OpenGLRenderer(6424): Flushing caches (mode 0)
12-22 23:57:00.793: D/AndroidRuntime(6424): Shutting down VM
12-22 23:57:00.793: W/dalvikvm(6424): threadid=1: thread exiting with uncaught exception (group=0x40ef99a8)
12-22 23:57:00.795: E/AndroidRuntime(6424): FATAL EXCEPTION: main
12-22 23:57:00.795: E/AndroidRuntime(6424): java.lang.NullPointerException
12-22 23:57:00.795: E/AndroidRuntime(6424):     at charge.system.genConServer.onPostExecute(GenConn.java:94)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at charge.system.genConServer.onPostExecute(GenConn.java:1)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at android.os.AsyncTask.finish(AsyncTask.java:631)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at android.os.Handler.dispatchMessage(Handler.java:107)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at android.os.Looper.loop(Looper.java:194)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at android.app.ActivityThread.main(ActivityThread.java:5371)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at java.lang.reflect.Method.invoke(Method.java:525)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-22 23:57:00.795: E/AndroidRuntime(6424):     at dalvik.system.NativeStart.main(Native Method)

我不明白为什么在该区域有nullPointer,并在浏览器上有数据。

1 个答案:

答案 0 :(得分:1)

你已经将你的HttpResponse设置为null我认为这会导致空指针异常。  试试这个, 而不是设置HttpResponse response=null

HttpResponse response = httpclient.execute(httppost);