2012-03-13 73 views
0

我的应用程序在模拟器上运行良好,但当它在真正的Android手机上时,它无法连接到互联网。电话使用EDGE/GPRS有效连接互联网。无法连接到真正的手机上的互联网(Android),但在仿真器上工作

这里的类:两个response()response_p()

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.BasicResponseHandler; 
import org.apache.http.impl.client.DefaultHttpClient; 

public class get_string { 


    public String response_p (String url) 
    { 


      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(url.toString()); 

      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      String response_str = null; 
      try { 
       response_str = client.execute(request, responseHandler); 
      } catch (ClientProtocolException e) { 

       response_str="NOT"; 
      } catch (IOException e) { 

       response_str="NOT"; 
      } 

      return response_str; 


    } 

    public String response (String str) 
    { 
     String res=""; 
     try { 
      URL url = new URL(str); 
      HttpURLConnection con = (HttpURLConnection) url 
       .openConnection(); 
      res=readStream(con.getInputStream()); 
      } catch (Exception e) { 
      res="NOT"; 
     } 



     return res; 



    } 

    private String readStream(InputStream in) throws IOException { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     String line = "",res=""; 
     while ((line = reader.readLine()) != null) { 
      res+=line; 
     } 

     return res; 
    } 




} 

相同的结果。

这里的清单文件:

 </application> 
     <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    </manifest> 

我怎样才能解决这个问题?

+0

如何从手机访问互联网?通过WiFi或GPRS? – Android 2012-03-13 14:54:03

+0

你知道它在哪里失败吗?所有的失败情况都会返回字符串“NOT”。为什么不记录或返回具体原因? – Rich 2012-03-13 14:55:37

+0

@Android手机正在使用GPRS – 2012-03-13 14:58:29

回答

1

我将response()和response_p()移动到了我的默认活动类,它工作! 我仍然不知道为什么,但两个功能在模拟器和真正的手机中都能很好地工作。

1

问题可能是SIM特定的或设备特定的。尝试使用不同的SIM卡或其他设备的应用程序。这将给出一个更好的想法和问题的人谁想投票我的回答我还没有获得评论员徽章,所以我写我的评论作为答案。您的网址也可能未被设备读取。

+0

为了支持这个答案,我在我的平板电脑上遇到了同样的问题,但没有在任何其他设备上,因此可能是设备或SIM专用的,就像@Dinesh所提到的那样。所以试试其他设备。 – Mxyk 2012-03-13 15:38:57

相关问题