这是我的工作代码示例..
您只需要在 AsyncTask类中运行以下代码并传递您的 URL,它将返回来自服务器的响应。
public String getResult(String url) {
Log.v(TAG, "Final Requsting URL is : :"+url);
String line = "";
String responseData = null;
try {
StringBuilder sb = new StringBuilder();
String x = "";
URL httpurl = new URL(url);
URLConnection tc= httpurl.openConnection();
/*URLConnection tc = twitter.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) tc;
httpConnection.setRequestMethod("POST");*/
//tc.setRe
//tc.setDoOutput(false);
BufferedReader in = new BufferedReader(
new InputStreamReader(tc.getInputStream()));
while ((line = in.readLine()) != null) {
sb.append(line + "\n");
x = sb.toString();
}
responseData = new String(x);
}
catch (UnknownHostException uh){
Log.v("NewWebHelper", "Unknown host :");
uh.printStackTrace();
}
catch (FileNotFoundException e) {
Log.v("NewWebHelper", "FileNotFoundException :");
e.printStackTrace();
}
catch (IOException e) {
Log.v("NewWebHelper", "IOException :");
e.printStackTrace();
}
catch (Exception e) {
Log.v("NewWebHelper", "Exception :");
e.printStackTrace();
}
return responseData;
}
还请确保您在清单文件中需要以下 Internet 权限。
<uses-permission android:name="android.permission.INTERNET"/>
让我知道你对此的评论!!