gpt4 book ai didi

android - NullPointerException :Parsing XML, 从 ByteArrayInputStream(string.getBytes()) 创建 InputStream

转载 作者:行者123 更新时间:2023-11-29 21:40:36 24 4
gpt4 key购买 nike

新的 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。感谢您的帮助!

最佳答案

此 logcat 消息:

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

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

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

// This should work...
String strEntity = EntityUtils.toString(resEntity); // read the Entity once!
Log.i("RESPONSE", strEntity);
return (strEntity);

关于android - NullPointerException :Parsing XML, 从 ByteArrayInputStream(string.getBytes()) 创建 InputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17222091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com