虽然代码有点长,但在我看来,大多数代码都非常简单。现在我正在编写一个android应用程序,它将数据发布到网站并将返回的HTML代码显示到TextView。首先我编写了一个简单的java POST方法,它在我的计算机上运行完美(在linux上,将用户名和密码发布到http ://forum.xda-developers.com/login.php
,它成功返回了HTML,但提示我用户名和密码不正确):
package com.app.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class Connection
{
String returnText = "";
String returnLine;
//data which is about to be posted
public String data = "vb_login_username=test&vb_login_password&securitytoken=guest&do=login";
public String returnHTML()
{
try {
URL url = new URL("http://forum.xda-developers.com/login.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((returnLine = rd.readLine()) != null) {
returnText += returnLine;
}
wr.close();
rd.close();
return returnText;
} catch (Exception e) {
return "ERROR: "+e.getMessage();
}
}
}
我使用以下方法测试了代码:
Connection net_start = new Connection();
System.out.println(net_start.returnHTML());
完美。 HTML成功返回,然后我去写一个android应用程序,它很简单,只有一个按钮和一个TextView。当我按下按钮时,如果成功,TextView 将显示返回的 HTML。这是主Activity.java的源代码
package com.app.main;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AndroidTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView displayResult = (TextView)findViewById(R.id.displayResult);
Button login = (Button)findViewById(R.id.login);
//set dispalyResult scrollable
displayResult.setMovementMethod(new ScrollingMovementMethod());
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Connection net_start = new Connection();
displayResult.setText(net_start.returnHTML());
}
});
}
}
非常简单的代码,与上面唯一的区别是我将返回的HTML显示设置在TextView上而不是命令行上。但我失败了。根据调试,我发现应用程序在 Connection.java 中的该行抛出异常:OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
,捕获的 e 表示:java .net.UnknownHostException:forum.xda-developers.com
,但我确实可以在手机上登录xda-developers网站(我在手机上使用adb
调试应用程序),什么是是怎么回事? android 不能使用URLConnection
吗?任何帮助表示赞赏:)
最佳答案
可以吗?
<uses-permission
android:name="android.permission.INTERNET" />
在您的Android应用程序的 list 文件中..
您的代码在我的情况下运行良好:
returnText
的结果是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en"><head><meta name="robots" content="noindex,follow" /><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><meta http-equiv="X-UA-Compatible" content="IE=9" ><meta name="keywords" content="XDA,MDA,Vario,HTC,Qtek,wallaby,wizard,tytn,hermes,alpine,prophet,i-mate,jam,dopod,tornado,typhoon,programming,coding,modding,wiki,forum,news,xdatools,xdaunlock,romkitchen,rom,development,lobster,unlock,imei" /><meta name="description" content="Android and Windows Mobile Developers - The Largest Community for Smartphone Hacks and Development of Apps" /><style type="text/css" id="vbulletin_css">/*** vBulletin 3.8.7 CSS* Style: 'XDA 2010'; Style ID: 16*/@import url("http://media.xda-developers.com/clientscript/vbulletin_css/style-e9bec9ef-00016.css");
关于java - java中的POST与android中的POST不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051970/