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. Phone has active internet connection using 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().
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!
I still don’t know why but both functions worked perfectly in emulator and real phone.