0

我在一个线程下执行网络任务。实际上我正在加载一个网页,该网页返回一个 xml 响应,我从中提取了一些值。我想返回服务的那些值。但我做不到。

线程类包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 消息

4

1 回答 1

0

您不能wait()在您的 UIThread 中执行 a,这与在 UIThread 中启动任务相同...尝试显示 aProgressDialog而不是并在其他线程完成后wait()关闭它。ProgressDialog

于 2013-01-30T13:44:47.717 回答