不显示RAW XML数据

时间:2015-03-25 18:06:37

标签: java android xml

我正在学习视频教程,而且我完成了与视频完全相同的东西,唯一的区别在于他们在Eclipse上做的视频和我在Android Studio上做的事情(我想它不应该影响结果)。无论如何,我想要做的是从Apple RSS获取原始XML数据到我的Android应用程序并将其显示在文本字段中......

我已添加:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

到清单文件

XML CODE:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />

</RelativeLayout>

主要活动代码:

package com.example.haziqsheikhlocal.top10appsofios;

import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends ActionBarActivity {
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text =(TextView) findViewById(R.id.textView1);
        new DownloadData().execute("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class DownloadData extends AsyncTask<String, Void, String>{

        String myXmlData; // This String Will Contain Our XML Data
        protected String doInBackground(String... urls) { // It Finds The First Url In BackGround;
            try{
                myXmlData = downloadXML(urls[0]); // It Downloads the first element in array Element;
            }
            catch (IOException e){
               e.printStackTrace();
                return "" ;
            }
            return null;
        }
        protected void onPostExecution(String result){
            Log.d("OnPostExecute", myXmlData);
            text.setText(myXmlData);
        }

        private String downloadXML (String theUrl) throws IOException {
            int BUFFER_SIZE = 2000;   // Buffer Size Constant
            InputStream is = null; // Declaring Input Stream
            String xmlContent = ""; // Creating empty xmlContent variable to store xml data from the web in furutre
            try {
                URL url = new URL(theUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Open The Connection with url given
                conn.setReadTimeout(10000);// Read time Out 10seconds
                conn.setConnectTimeout(15000);// connection timeout 15 s
                conn.setRequestMethod("GET"); // Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions.
                conn.setDoInput(true);
                int response = conn.getResponseCode();
                Log.d("DownloadXML", "The Ressponse Code Is: " + response);
                is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                int charRead;
                char[] inputBuffer = new char[BUFFER_SIZE];
                try {
                    while ((charRead = isr.read(inputBuffer)) > 0){
                        String readString = String.copyValueOf(inputBuffer, 0 , charRead);
                        xmlContent += readString;
                        inputBuffer = new char[BUFFER_SIZE];
                    }
                    return xmlContent;
                }

                catch (IOException e){
                    e.printStackTrace();
                    return null;
                }

            }finally {
                if(is != null) {
                    is.close();
                }
            }

        }

    }
}

设备LogCat显示没有错误&amp;显示响应代码200。  设备LogCat:

03-25 22:53:09.237    1144-1144/com.example.haziqsheikhlocal.top10appsofios I/art﹕ Not late-enabling -Xcheck:jni (already on)
03-25 22:53:12.688    1144-1162/com.example.haziqsheikhlocal.top10appsofios D/OpenGLRenderer﹕ Render dirty regions requested: true
03-25 22:53:12.718    1144-1144/com.example.haziqsheikhlocal.top10appsofios D/﹕ HostConnection::get() New Host Connection established 0xa7b419c0, tid 1144
03-25 22:53:12.771    1144-1144/com.example.haziqsheikhlocal.top10appsofios D/Atlas﹕ Validating map...
03-25 22:53:13.058    1144-1162/com.example.haziqsheikhlocal.top10appsofios D/﹕ HostConnection::get() New Host Connection established 0xa7b41bf0, tid 1162
03-25 22:53:13.122    1144-1162/com.example.haziqsheikhlocal.top10appsofios I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-25 22:53:13.194    1144-1162/com.example.haziqsheikhlocal.top10appsofios D/OpenGLRenderer﹕ Enabling debug mode 0
03-25 22:53:13.256    1144-1162/com.example.haziqsheikhlocal.top10appsofios W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-25 22:53:13.256    1144-1162/com.example.haziqsheikhlocal.top10appsofios W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa7b3ba40, error=EGL_SUCCESS
03-25 22:53:14.100    1144-1156/com.example.haziqsheikhlocal.top10appsofios I/art﹕ Background sticky concurrent mark sweep GC freed 3424(291KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 968KB/1135KB, paused 1.420ms total 737.728ms
03-25 22:53:15.313    1144-1161/com.example.haziqsheikhlocal.top10appsofios D/DownloadXML﹕ The Ressponse Code Is: 200

ADB LogCat显示此错误:

DeviceMonitor: Adb connection Error:An existing connection was forcibly closed by the remote host
ddms: null
java.nio.BufferOverflowException
    at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:206)
    at com.android.ddmlib.JdwpPacket.movePacket(JdwpPacket.java:235)
    at com.android.ddmlib.Debugger.sendAndConsume(Debugger.java:347)
    at com.android.ddmlib.Client.forwardPacketToDebugger(Client.java:698)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:344)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

ddms: Client data packet exceeded maximum buffer size [Client pid: 1272]
DeviceMonitor: Adb rejected connection to client '1537': closed
DeviceMonitor: Adb connection Error:An existing connection was forcibly closed by the remote host
DeviceMonitor: Connection attempts: 1
DeviceMonitor: Connection attempts: 2
DeviceMonitor: Connection attempts: 3
DeviceMonitor: Connection attempts: 4
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline
PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline

我要解决的问题是我去了终端并使用adb kill-server然后使用adb start-server。我再次运行模拟器,但仍然没有运气! 请帮帮我 。

注意:当我使用gennymotion并且我能够使用模拟器浏览器和冲浪网站时,也会发生同样的事情。

0 个答案:

没有答案