-1

I am new to coding in general, what am i doing wrong? Somebody told me to make async task but i have no idea how to do it... My final aim is to fetch data from an xml page after login and then display it on my device.

------------------------JAVA-CODE--------------------------------------

public class MainActivity extends Activity { TextView textViewXML;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    HttpURLConnection urlConnection = null;
    final String LOG_TAG = MainActivity.class.getSimpleName();
    String[] params={"Acheivers"};

    try {
        final String FORECAST_BASE_URL="http://www.unimaxindia.net/webservice.asmx?";
        final String QUERY_PARAM="op";
        final String FORMAT_PARAM="mode";

        Uri builtUri=Uri.parse(FORECAST_BASE_URL).buildUpon()
                .appendQueryParameter(QUERY_PARAM, params[0])
                .appendQueryParameter(FORMAT_PARAM,"xml")
                .build();

        URL url = new URL(builtUri.toString());

        Log.v(LOG_TAG, "Built URI" + url);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();
        textViewXML =(TextView)findViewById(R.id.textViewXML);
        InputStream inStream = urlConnection.getInputStream();
        /*
        * this.getAssets().open("AgentDetail.xml");
        *
        *
        */
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = domFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(inStream);
        if (doc.hasChildNodes()) {
            printNote(doc.getChildNodes());
        }

    } catch (Exception e) {
        textViewXML.setText(textViewXML.getText().toString() + e.getMessage());
    }
}

private void printNote(NodeList nodeList) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        // make sure it's element node.
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            textViewXML.setText( tempNode.getTextContent());
        }
    }
}

@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);
}

}

------------------------LOGCAT------------------------------------------

07-01 13:30:18.455    5298-5316/com.xmlparsing.tnh.sampleagent W/art﹕ Suspending all threads took: 13.264ms
07-01 13:30:18.566    5298-5298/com.xmlparsing.tnh.sampleagent D/AndroidRuntime﹕ Shutting down VM
07-01 13:30:18.594    5298-5298/com.xmlparsing.tnh.sampleagent E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.xmlparsing.tnh.sampleagent, PID: 5298`enter code here`
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xmlparsing.tnh.sampleagent/com.xmlparsing.tnh.sampleagent.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
            at com.xmlparsing.tnh.sampleagent.MainActivity.onCreate(MainActivity.java:62)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  • You're using, inside the catch, a variable initialized in the try block. You need to read a book about the basics of Java. – 2Dee Jul 02 '15 at 07:53
  • @2Dee yeah you are right... i am new to programming... but i fixed that... a new problem is that it says something about **Cursor not at right position**. – Shantanu Ghosh Jul 03 '15 at 07:14

1 Answers1

-1

First you should call network related operation in Async Task see: Android: AsyncTask to make an HTTP GET Request?

A lot more to learn I'll suggest follow android training here: http://developer.android.com/training/index.html

Community
  • 1
  • 1
himanshu munjal
  • 321
  • 1
  • 7
  • That's all very nice but if your answer is not even attempting to solve the problem exposed in the question, then it should be a comment, not an answer ... – 2Dee Jul 01 '15 at 08:26