0

全部,

这是这篇文章的延续。

我做了一些调试,这是我的发现。

首先是一些代码:

class MainActivity extends Activity
{
    SAXParser parser = spf.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    ProductXMLParser productParser = new ProductXMLParser();
    reader.setContentHandler( productParser );
    reader.parse( new Communicator().getXML( params, true ) );
}
class Communicator
{   
    public InputSource getXML(List<NameValuePair> params, boolean writeToFile)
{
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost( categories_url );
    InputSource source = null;
    try
    {
        post.setEntity( new UrlEncodedFormEntity( params ) );
        HttpResponse responce = client.execute( post );
        StatusLine status = responce.getStatusLine();
        if( status.getStatusCode() != 200 )
            throw new IOException( "Invalid responce from server: " + status.toString() );
        InputStream is = responce.getEntity().getContent();
        /*if( writeToFile )
        {
            final File file = new File( Environment.getExternalStorageDirectory() + File.separator + "products.txt" );
            if( !file.exists() )
                file.createNewFile();
            final OutputStream stream = new FileOutputStream( file );
            final byte[] buffer = new byte[1024];
            int read;
            while( ( read = is.read( buffer ) ) != -1 )
                stream.write( buffer, 0, read );
            stream.flush();
            stream.close();
            Reader reader = new InputStreamReader( new FileInputStream( file ) );
            source = new InputSource( reader );
        }
        else*/
            source = new InputSource( is );
    }
    catch( UnsupportedEncodingException e )
    {
        e.printStackTrace();
    }
    catch( IOException e)
    {
        e.printStackTrace();
    }
    return source;
}
public Bitmap loadProductPicture(String url) throws IOException
{
    InputStream stream = null;
    Bitmap bitmap = null;
    try
    {
        stream = new URL( url ).openStream();
        bitmap = BitmapFactory.decodeStream( stream );
    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    finally
    {
        stream.close();
    }
    return bitmap;
}

这是我尝试过的原始代码。在调试器下检查,我看到 getStatusCode() 返回 200,这意味着一切正常,但正如链接线程中所解释的那样,解析器阻塞并抛出异常,读取结果流中某处的数据。

为了确保一切正常,我添加了写入文件代码(上面注释掉的部分)。尝试运行修改后的版本没有例外,一切都被正确解析。我可以在设备上看到结果。我的猜测是,即使 Web 服务器返回 200,缓冲区中仍有一些数据要读取

现在我的问题是:我如何检查这种情况?或者它是服务器端实现的错误?我收到的 xml 文件没有格式错误并且是完整的,但是通过文件保存例程我看到它确实通过了保存循环几次。或者我可以设置一个参数来适当地设置 http 客户端缓冲区?

感谢您的任何建议。

[编辑]

06-27 11:53:08.702: E/AndroidRuntime(3114): FATAL EXCEPTION: main
06-27 11:53:08.702: E/AndroidRuntime(3114): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skylite.irabwah/com.skylite.irabwah.IRabwah}: java.lang.NullPointerException
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.os.Looper.loop(Looper.java:150)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.app.ActivityThread.main(ActivityThread.java:4385)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at java.lang.reflect.Method.invoke(Method.java:507)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at dalvik.system.NativeStart.main(Native Method)
06-27 11:53:08.702: E/AndroidRuntime(3114): Caused by: java.lang.NullPointerException
06-27 11:53:08.702: E/AndroidRuntime(3114):     at com.skylite.irabwah.Communicator.loadProductPicture(Communicator.java:88)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at com.skylite.irabwah.ProductXMLParser.characters(ProductXMLParser.java:168)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at com.skylite.irabwah.IRabwah.onCreate(IRabwah.java:81)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-27 11:53:08.702: E/AndroidRuntime(3114):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
06-27 11:53:08.702: E/AndroidRuntime(3114):     ... 11 more

class ProductXMLParser
{
    @Override
    public void characters(char ch[], int start, int length)
    {
        if( inProductPicture )
        {
        Communicator comm = new Communicator();
        try
        {
            Bitmap bmp = comm.loadProductPicture( new String( ch, start, length ) );
        product.setProduct_picture( bmp );
            }
            catch( IOException e)
            {
                e.printStackTrace();
            }
    }
    }
}

[/编辑]

[编辑2]

<?xml version="1.0"?>
<rss version="2.0">
<products>
<product>
<product_id>72</product_id>
<cat_id>59</cat_id>
<cat_name>Foods &amp; Meals</cat_name>
<product_code>IR-MI-01</product_code>
<product_name>MITHAI</product_name>
<description>   MITHAI pack contain's Combination of different delicious and delightful sweets such as Gulab jaamun, rass gulla, Barfi, laddu bake in pure desi ghee. Also sugar less sweets variety is available.</description>
<rating>n/a</rating>
<image_url>http://irabwah.com/image/data/1_kg_mithai.jpg</image_url>
<price>4.99</price>
<discount>0</discount>
<stock_status>In Stock</stock_status>
<stock_quantity>1006</stock_quantity>
<weight>0</weight>
<length>n/a</length>
<width>n/a</width>
<height>n/a</height>
<colour>n/a</colour>
<size>n/a</size>
<material>n/a</material>
<pattern>n/a</pattern>
</product>
<product>
<product_id>59</product_id>
<cat_id>59</cat_id>
<cat_name>Foods &amp; Meals</cat_name>
<product_code>IR-PZ-03</product_code>
<product_name>Master Pizza, Nuggets, Fries &amp; Coke</product_name>
<description>   Master Pizza with 5 types of sausage and tasty toppings: Chicken, Chees, Mushroom, Tomato, Onion, Pizza Saus &amp;amp; Sweet Corn, with 6 Chicken Nuggets, Large Fries and 1.5 ltr Coke.</description>
<rating>n/a</rating>
<image_url>http://irabwah.com/image/data/Master_pizza.jpg</image_url>
<price>15.99</price>
<discount>0</discount>
<stock_status>In Stock</stock_status>
<stock_quantity>1001</stock_quantity>
<weight>0</weight>
<length>n/a</length>
<width>n/a</width>
<height>n/a</height>
<colour>n/a</colour>
<size>n/a</size>
<material>n/a</material>
<pattern>n/a</pattern>
</product>

[/EDIT2]

4

0 回答 0