[英]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.