我有這個挑戰,一直讓我瘋狂,不知道如何解決它。我開發了一個android應用程序,它會使用post將一些數據發佈到遠程URL。這裏是我的方法來處理java.net.UnknownHostException與Android應用
public static String fetchStringFromRemotePOST(String url,List<NameValuePair> nameValuePairs){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
Log.i("URL",url);
String line = "";
String content = "";
try{
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = rd.readLine()) != null) {
content += line;
}
}catch(Exception e){
Log.d("String Fetch ERROR",e.toString());
}
return content;
}
,並調用該方法
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("post","okpokor"));
String url = "http://wwww.host.com/post.php";
String response = UtilMethod.fetchStringFromRemotePOST(url,nameValuePairs);
還有,我已經包括在Android清單文件中的Internet權限
<uses-permission
android:name="android.permission.INTERNET" />
<application
...
我已經通過以前在其他線程上討論過的相同問題,並且沒有任何建議對我有用,建議像重新啓動Eclipse和AVD。
真的需要幫助!
你不應該用+連接字符串= ,最好使用StringBuilder而不是 – koljaTM
你的代碼看起來沒問題。你有沒有嘗試過使用模擬器中的瀏覽器來檢查模擬器是否具有Internet連接? – Thommy
post stacktrace – njzk2