포털에서 검색을 한 예제는 하나 같이 바로 돌아 가지가 않는데
여기에는 이유가 있었으니 바로 버젼이 업그레이드 되면서 아래와 같은 문제가 발생한것이다.
첫번째 android.os.NetworkOnMainThreadException 발생
메인 스레드에서 IO작업시 발생 하며 스레드 등을 생성하여 호출하는것으로 에러를 잡을 수있다.
두번째 android.permission.INTERNET permission while debugging on device
인터넷 연결 권한 문제로 AndroidManifest.xml에 권한 설정을 해주면 된다.
해서 작성된 최종 예제는 아래와 같다.
-------------------------------------------------------------------------------
package com.example.testmaven2;
import java.util.concurrent.ExecutionException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
CallRemote cr = new CallRemote();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(call_process());
}
public String call_process(){
AsyncTask<String, String, String> at = cr.execute("","","");
String s = null;
try {
s = at.get();
} catch (InterruptedException e) {e.printStackTrace();
} catch (ExecutionException e) {e.printStackTrace();
}
return s;
}
}
class CallRemote extends AsyncTask<String, String, String>{
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String SOAP_METHOD = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
public String doInBackground(String... params) {
String results = "";
try {
SoapObject request = new SoapObject(NAMESPACE, SOAP_METHOD);
request.addProperty("Celsius", "5");//전달 파라미터
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet=true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
results = result.toString();
} catch (Exception e) {
results = ""+e.fillInStackTrace();
Log.d("exception",""+e.fillInStackTrace());
}
return results;
}
}
-------------------------------------------------------------------------------
AndroidManifest.xml 에 아래 내용 추가
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
-------------------------------------------------------------------------------