mylibrary
というアンドロイドスタジオライブラリモジュールで非同期タスクを定義しました。私はmylibrary
の非同期タスクとしてクラスgetDeviceIP
を定義しました。そのライブラリモジュールを別のプロジェクトtestMyLibrary
にインポートしました。 MainメソッドからtestMyLibrary
に非同期メソッドを呼び出そうとすると、エラーが発生し、シンボルexecute()を解決できません。私はクラスgetDeviceIPをtestMyLibrary
と同じプロジェクトに置くと問題なく動作します。しかし、ライブラリにメソッドが存在する場合はgetDeviceIP
が認識されますが、getDeviceip.execute()
にはメソッドが認識されません。ライブラリ内で定義されたクラスが非同期タスクを実行しない
てMyLibraryコードは:
package com.example.ramesh.mylibrary;
import android.util.Log;
import android.os.AsyncTask;
public class getDeviceIP {
private String Url;
private Integer tos;
public getDeviceIP(String url, Integer typeOfService) {
Url = url;
tos = typeOfService;
}
protected String doInBackground(Void... params) {
//now call your ipify method which is doing the networking or calling a method that might be doing the networkign
//getContents("http://localhost:8080");
String ip = "";
if (tos == 1) {
ip = getipify(Url);
Log.i("Service = ", "Get IP address");
} else {
Log.i("Service = ", "Not Defined");
ip = ">>>>Not Defined<<<<";
}
Log.i("ip after getipify call ",ip);
return ip;
}
private String getipify(String url) {
String IP="";
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL(url).openStream(), "UTF-8").useDelimiter("\\A")) {
IP = s.next();
System.out.println("My current IP address is " + IP.toString());
//IP = s.next();
} catch (java.io.IOException e) {
System.out.println("======");
System.out.println(e);
System.out.println("======");
}
Log.i("Test","IP");
return(IP);
}
}
主な活動は、ラインにおいて
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String myUrl = "https://api.ipify.org";
String result = "";
try {
result = new getDeviceIP(myUrl,1).execute().get();
} catch (Exception e) {
System.out.println("Execution Excepton" + e);
}
Log.i("IP in main = ", result);
}
}
あり、**)(実行**認識されません。
あなたのお手伝いをお待ちしております。
getDeviceIPクラスはAsyncTask –