1

新的Android编程。

空指针发生在这一行:

 String toBeConverted = XmlFile(myURL, "username@user.com",
                "password");
    InputStream is = new ByteArrayInputStream(toBeConverted.getBytes());

这是我用来获取字符串的方法,该方法有效,因为我从服务器获得了正确的响应

06-20 15:15:40.804: I/RESPONSE(2270): <?xml version="1.0" encoding="utf-8"?>
06-20 15:15:40.804: I/RESPONSE(2270): <CarObject>
06-20 15:15:40.804: I/RESPONSE(2270):   <ResultCode>0</ResultCode>
06-20 15:15:40.804: I/RESPONSE(2270):   <ResultMessage>success</ResultMessage>
06-20 15:15:40.804: I/RESPONSE(2270):   <Cardata xsi:type="ArrayOfCarStatus">
06-20 15:15:40.804: I/RESPONSE(2270):     <CarStatus> Car
06-20 15:15:40.804: I/RESPONSE(2270):       <CarID>241</CarID>
06-20 15:15:40.804: I/RESPONSE(2270):       <CarName>ASC</CarName>
06-20 15:15:40.804: I/RESPONSE(2270):     </CarStatus>
06-20 15:15:40.804: I/RESPONSE(2270):   </CarData>
06-20 15:15:40.804: I/RESPONSE(2270): </CarObject>

这是我用来获取 XML 的方法

public String XmlFile(String url, String username, String password) {

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("user", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity resEntity = response.getEntity();

            Log.i("RESPONSE", EntityUtils.toString(resEntity));
            return EntityUtils.toString(resEntity);

        } catch (ClientProtocolException e) {

            Log.e("Log, ClientProtocol Exception", e.toString());
        } catch (IOException e) {
            Log.e("Log, IO Exception", e.toString());
        } catch (IllegalArgumentException e) {
            Log.e("Log, IllegalArgument Exception", e.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("a", "XmlFile returning null string");
        return null;

    }

日志猫:

06-20 15:15:40.819: W/System.err(2270):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-20 15:15:40.819: W/System.err(2270):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-20 15:15:40.819: W/System.err(2270):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 15:15:40.819: W/System.err(2270):     at android.os.Looper.loop(Looper.java:137)
06-20 15:15:40.819: W/System.err(2270):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 15:15:40.819: W/System.err(2270):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 15:15:40.827: W/System.err(2270):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 15:15:40.827: W/System.err(2270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 15:15:40.827: W/System.err(2270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 15:15:40.827: W/System.err(2270):     at dalvik.system.NativeStart.main(Native Method)
06-20 15:15:40.827: E/a(2270): XmlFile returning null string
06-20 15:15:40.827: D/AndroidRuntime(2270): Shutting down VM
06-20 15:15:40.827: W/dalvikvm(2270): threadid=1: thread exiting with uncaught exception (group=0x40ccc930)
06-20 15:15:40.835: E/AndroidRuntime(2270): FATAL EXCEPTION: main
06-20 15:15:40.835: E/AndroidRuntime(2270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bentseytlin.maps/com.bentseytlin.maps.MainActivity}: java.lang.NullPointerException
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.os.Looper.loop(Looper.java:137)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at dalvik.system.NativeStart.main(Native Method)
06-20 15:15:40.835: E/AndroidRuntime(2270): Caused by: java.lang.NullPointerException
06-20 15:15:40.835: E/AndroidRuntime(2270):     at com.bentseytlin.maps.MainActivity.onCreate(MainActivity.java:84)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.Activity.performCreate(Activity.java:5104)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-20 15:15:40.835: E/AndroidRuntime(2270):     ... 11 more

更新: 当 log.i 消息按预期出现时,我不确定为什么我的 XmlFile 函数返回 null。谢谢您的帮助!

4

1 回答 1

2

此 logcat 消息:

06-20 15:15:40.827: E/a(2270): XmlFile 返回空字符串

表明您的 XmlFile() 函数返回 null,并导致随后的空指针异常。

很可能您 LOGGING 实体,并且它是一个流,这意味着您随后无法再次读取它。删除 LOGGING 语句,您的代码可能会起作用。有关 HttpEntity 和可重复流的讨论,请参阅http://developer.android.com/reference/org/apache/http/HttpEntity.html#isRepeatable()

// This should work...
String strEntity = EntityUtils.toString(resEntity);   // read the Entity once!
Log.i("RESPONSE", strEntity);
return (strEntity);
于 2013-06-20T19:31:59.570 回答