开个子线程  , 可好 ? Runable ?

解决方案 »

  1.   

    自己做了个线程类,还是在2.x可以,在4.x不行啊。
    package com.example.xhttpget02;import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;import android.util.Log;public class MyThread extends Thread{
    private static String myString = null;

    public MyThread(){
    super();
    }

    public void run(){
    HttpURLConnection conn = null;
         InputStream inputStream = null;
             
         try{
    URL url = new URL("http://services.odata.org/Northwind/Northwind.svc/Customers('ALFKI')/ContactName/$value");  

    conn = (HttpURLConnection) url.openConnection();  
    inputStream = new BufferedInputStream(conn.getInputStream());          

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    String result = "";
    String line = "";

    while ((line=reader.readLine()) != null)  {
    result = result + line;
    }

    myString = result;

         }catch(MalformedURLException e){
         e.printStackTrace();
         }catch(IOException e){
         e.printStackTrace();
    }catch(Exception e){
         Log.d("01", "have error... ");
    e.printStackTrace();
    }finally { 
        try{
         inputStream.close();
         conn.disconnect();
         }catch (IOException e){
         e.printStackTrace();
         }
         
    }

    }

    public String toString(){
    return myString;
    }

    }能否给个通过的代码。可以在给分。谢谢
      

  2.   

    07-26 10:17:47.980: W/System.err(1577): android.os.NetworkOnMainThreadException
    07-26 10:17:48.000: W/System.err(1577):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    07-26 10:17:48.000: W/System.err(1577):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    07-26 10:17:48.000: W/System.err(1577):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    07-26 10:17:48.010: W/System.err(1577):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
    07-26 10:17:48.010: W/System.err(1577):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    07-26 10:17:48.020: W/System.err(1577):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    07-26 10:17:48.030: W/System.err(1577):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
    07-26 10:17:48.030: W/System.err(1577):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    07-26 10:17:48.030: W/System.err(1577):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    07-26 10:17:48.030: W/System.err(1577):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
    07-26 10:17:48.050: W/System.err(1577):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
    07-26 10:17:48.060: W/System.err(1577):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
    07-26 10:17:48.070: W/System.err(1577):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
    07-26 10:17:48.070: W/System.err(1577):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
    07-26 10:17:48.080: W/System.err(1577):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)好像是联网的线程不对啊。
      

  3.   

    在新线程外边new一个Handler类,例如 Handler h = new Handler(); 然后 h.post(Runnable r);  参数的那个Runnable用你具体的那个Runnable对象。
      

  4.   

    非常感谢各位.用AsyncTask解决..
    回贴都给分了.
    再次谢谢