-1

I am trying to make a call to a web service when I click the button, the button part and input works now i am trying to actually call web service, but i need Threads to do so, but i don't know them. People are saying to use AsyncTask.

LogCat log for callWS: callWS: "http://pastebin.com/nuLnPQz8"

callWS FUNCTION:: It will allways crash because it requires Threads.

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" >

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Pošlji "
        android:onClick = "callWS"/>


    <EditText
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:ems="10" />

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Text1"
        android:layout_marginTop="23dp"
        android:layout_toLeftOf="@+id/Text1"
        android:text="Output: "
        tools:context=".Activity123" />

</RelativeLayout>

Activity123.java code:

package com.test123;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.test123.R;

public class Activity123 extends Activity 
   {
      private static final String SOAP_ACTION = "http://192.168.0.25/webapplication1/ws.asmx";
      private static final String METHOD_NAME = "HelloWorld";
      private static final String NAMESPACE = "http://microsoft.com/webservices/";
      private static final String URL = "http://192.168.0.25/webapplication1/ws.asmx?WSDL";
      /** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) 
         {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_activity123);
         }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) 
         {
            getMenuInflater().inflate(R.menu.activity_activity123, menu);
            return true;
         }

  public void TEST(View button_one)
     {
        //int a = 2 + 3; // Runs fine
        //TextView text = (TextView) findViewById(R.id.output); // TextView text = (TextView) findViewById(R.id.button_one); // Runs fine
        //text.setText(String.valueOf(a)); IGNORE THIS ABOVE SECTION

        et = (EditText) findViewById(R.id.Text1); // et is our editText 
        String input = et.getText().toString(); // 

        TextView text = (TextView) findViewById(R.id.output); 
        text.setText( String.valueOf(input) ); 
        //setContentView(R.layout.secondary_activity);

        klicWS(input);
     }  

        public void klicWS(String input) // We receive input variable from TEST
           {
              //Thread networkThread = new Thread(); Threads like this... but this ain't correct....

              SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);                       
              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              envelope.setOutputSoapObject(request);
              envelope.dotNet = true;
              envelope.encodingStyle = SoapSerializationEnvelope.XSD;

              HttpTransportSE HT = new HttpTransportSE(URL);

              try 
                 {
                    HT.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                    //Object result = (Object)envelope.getResponse(); // maybe important ?

                       {
                          TextView text = (TextView) findViewById(R.id.output);
                          text.setText( "Message from WS: "+response.toString()  );

                          //TextView tv = new TextView(this); this is from an example
                          //tv.setText( "Message :"+response.toString() ); // from example
                          setContentView(R.layout.secondary_activity); // The second one...
                       }
                 } 

             catch (Exception e) 
                {
                   e.printStackTrace();
                }
     } 

}

SCREENSHOT OF STUFF INSIDE IDE(Eclipse), alongside that strings.xml for assistance: http://www.filedropper.com/ideplusstringsxml enter image description here

Primoz128
  • 1
  • 2
  • Please can you put those files somewhere like pastebin? I can't actually view them to help you without signing up at 4shared. Here is a link for you: http://pastebin.com/ Thanks! – ThePerson Oct 22 '12 at 09:31
  • Updated the logs, but for the screenshot i forgot the name of website i use that USED TO not require anything at all, but now it needs a simple login and it's great... it's something+shared +sharing... Anyone know a good image share website ? – Primoz128 Oct 22 '12 at 09:42
  • Tried imgur.com, but it just stops at 13% in firefox and at 0% on explorer... – Primoz128 Oct 22 '12 at 09:56
  • DONE http://www.filedropper.com/ideplusstringsxml – Primoz128 Oct 22 '12 at 10:27
  • I guess ill have to create another question... all answers in first hour then nothing for next 2... – Primoz128 Oct 22 '12 at 11:47
  • Oh come on people... i can't re-ask... – Primoz128 Oct 22 '12 at 15:03
  • Please someone help me out... i need this done really... im desperate... – Primoz128 Oct 22 '12 at 15:04
  • I tried to edit your question, but you better edit it again to show only the not solved yet problems, with the related errors from [logcat](http://stackoverflow.com/a/3280126/469983). – Adinia Oct 23 '12 at 07:49
  • I made some progress on my own yeh... WHAT I NEED now is the THREAD part... i got no idea... this tutorial does not help me http://javatutorialspoint.blogspot.com/2012/02/android-web-service-access-using-ksoap2.html cause... it's outdated. Ok ill edit – Primoz128 Oct 23 '12 at 08:40
  • well i edited this yesterday already, but website often goes page not found after doing it... – Primoz128 Oct 23 '12 at 08:46
  • Noone is gona see this anyways, since it's not on any kind of front page... ill just stay at 1 single 100 line program for a week... – Primoz128 Oct 23 '12 at 09:02
  • I think i edited somewhere like that... -.- – Primoz128 Oct 23 '12 at 09:40
  • Others ask for entire 100 + lines of code, and i just want it threaded and that's such a problem ? Well that's extremely rude. – Primoz128 Oct 23 '12 at 09:54
  • And that change wasn't mine, eclipse acts weird everyhere and then, does stuff on it's own... Also log cat isn't relevant anymore, we aren't searching for an error. – Primoz128 Oct 23 '12 at 10:01

3 Answers3

1

This is a common mistake. You call the TextView's setText method with an integer argument

int a = 2 + 3; // Runs fine
TextView text = (TextView) findViewById(R.id.button_one); 
text.setText(a); // makes ERROR

The setText method with an integer argument is designed to take a resource ID (R.string.some_string_key).

That is why the logs says

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5

You try to get a String from strings.xml from the generated R.java file with an ID of 5

Just replace the line that fails with

text.setText(String.valueOf(a)); // should work
Glenn Bech
  • 6,103
  • 4
  • 39
  • 56
  • No problem. This is why your app crashes. – Glenn Bech Oct 22 '12 at 10:07
  • I was 3 days hitting the wall with these problems... i will continue doing so until this app is finished and works connecting to any WS, god im just 17 and he got me that to do... (he got a PhD... ) – Primoz128 Oct 22 '12 at 10:12
0

I think the problem is this line.

TextView text = (TextView) findViewById(R.id.button_one);

You convert your BUTTON button_one to a TEXTVIEW. I don't know if this is possible.

Try to use

TextView text = (TextView) findViewById(R.id.output);

and test if your code would work.

JackTools.Net
  • 734
  • 6
  • 13
0

When the button is clicked you are trying to do some network related operations. In Android, any network related operations should not be done in the main UI thread.

So on the button click either start a new thread and get the information from network or use Async Task.

Thanks & Regards, Rajamanickem. S

Rajamanickem
  • 162
  • 4