我的應用程序在模擬器上運行良好,但當它在真正的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>
我怎樣才能解決這個問題?
如何從手機訪問互聯網?通過WiFi或GPRS? – Android 2012-03-13 14:54:03
你知道它在哪裏失敗嗎?所有的失敗情況都會返回字符串「NOT」。爲什麼不記錄或返回具體原因? – Rich 2012-03-13 14:55:37
@Android手機正在使用GPRS – 2012-03-13 14:58:29