简体   繁体   English

无法在真实手机(Android)上连接到互联网,但可以在模拟器上运行

[英]Can't connect to internet on real phone (Android) but works on emulator

My app is working well on emulator, but when it's on real android phone (sony erricsson xperia, sony ericsson xperia mini) it can't connect to internet. 我的应用程序在模拟器上运行良好,但当它在真正的Android手机(索尼爱立信xperia,索尼爱立信xperia迷你)它无法连接到互联网。 Phone has active internet connection using EDGE/GPRS. 手机使用EDGE / GPRS进行有效的互联网连接。

Here's the class: 这是班级:

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;
    }




}

Same result for both response() and response_p() . response()response_p()结果相同。

Here's the manifest file: 这是清单文件:

        </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>

How can I solve this problem? 我怎么解决这个问题?

I moved both response() and response_p() to my default activity class and it worked! 我将response()和response_p()都移动到我的默认活动类,它运行了! I still don't know why but both functions worked perfectly in emulator and real phone. 我仍然不知道为什么,但这两个功能在模拟器和真正的手机中完美运行。

The problem could be SIM specific or device specific. 问题可能是SIM特定的或设备特定的。 Try the app using a different SIM card or some other device. 使用其他SIM卡或其他设备试用该应用。 This will give a better idea of the problem and the people who want to down vote my answer I still haven't gained a commentator badge, so I'm writing my comment as an answer. 这将更好地了解问题以及想要对我的答案进行投票的人我还没有获得评论员徽章,所以我正在写我的评论作为答案。 It is also possible that your URL is not being fetched by the device. 您的URL也可能未被设备提取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Button在真实手机上不起作用,但在android studio模拟器上起作用 - Button doesn't work on real phone, but works on android studio emulator 真正的手机可以工作,但是当我在Android项目上工作时,模拟器无法在intellij中运行 - Real phone works but emulator can't be run in intellij idea when I work on an Android project Android:Internet可以在模拟器中运行,但不能在我的手机上运行 - Android: Internet works in emulator but not on my phone 无法从Android Studio上的Android模拟器连接到互联网 - Can't connect to internet from Android Emulator on Android Studio 我无法使用Android 4.0连接互联网模拟器 - I can't connect internet emulator with android 4.0 Android可穿戴模拟器无法连接usb连接手机 - Android wearable emulator can't connect with usb connected phone 可以在真人手机上使用Android模拟GPS吗? (没有模拟器) - Can the GPS be emulated in Android on a real phone? (no emulator) Android 2.2模拟器无法连接互联网,但4.0.3模拟器可以连接 - Android 2.2 Emulator can not connect internet but 4.0.3 emulator can connect Eclipse Android Emulator无法连接到互联网 - Eclipse Android Emulator doesn't connect to internet Android模拟器互联网连接问题-真实设备正常工作。 - Android emulator internet connection issues - real device works.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM