のスレッドでネットワーク タスクを実行しています。実際には、いくつかの値を抽出する xml 応答を返す Web ページを読み込んでいます。これらの値をサービスに戻したいのですが、それができません。
スレッド クラス パッケージ com.example.googledrivetest;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.os.Environment;
import android.util.Log;
public class CellThread extends Thread{
String Latitude,Longitude;
final String TAG="CellThread";
public CellThread(){
}
public void run() {
try {
URL url = new URL("www.example.com");
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"somefile.ext");
if(!file.exists()) file.createNewFile();
Log.v(TAG,urlConnection.getResponseMessage());
InputStream inputStream = urlConnection.getInputStream();
/*********************************************************************/
StringBuilder inputStringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
while(line != null){
inputStringBuilder.append(line);inputStringBuilder.append('\n');
line = bufferedReader.readLine();
}
Log.v(TAG,inputStringBuilder.toString());
/*********************************************************************/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("rsp");
/** Assign textview array lenght by arraylist size */
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList cellList = fstElmnt.getElementsByTagName("cell");
Element cellElement = (Element) cellList.item(0);
cellList = cellElement.getChildNodes();
this.Latitude=cellElement.getAttribute("lat");
this.Longitude=cellElement.getAttribute("lon");
Log.v(TAG,"lat:"+Latitude+" LON:"+Longitude);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //you can write here any link
}
public String getLat() {
return Latitude;
}
public String getLng() {
return Longitude;
}
}
サービスクラスの機能
public synchronized void Getdata(){
final String Lat;
String Lng;
CellThread t = new CellThread();
t.start();
try {
synchronized (t) {
t.wait();
}
Log.v(TAG," LAT:"+t.getLat()+" lon"+t.getLng());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
アプリケーションが停止し、ANR メッセージが表示される