我一直在试图弄清楚如何在Android中发送post方法。这就是我的代码的样子:
public class HomeActivity extends Activity implements OnClickListener {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.text);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
HttpPost httpMethod = new HttpPost("http://www.example.com/");
httpMethod.addHeader("Accept", "text/html");
httpMethod.addHeader("Content-Type", "application/xml");
AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
String result = null;
try {
HttpResponse response = client.execute(httpMethod);
textView.setText(response.toString());
HttpEntity entity = response.getEntity();
Log.i(HomeActivity.class.toString(), result);
textView.setText("Invoked webservice");
} catch (IOException e) {
e.printStackTrace();
Log.e(HomeActivity.class.toString(), e.getMessage());
textView.setText("Something wrong:" + e.getMessage());
}
}
}
这是我得到的例外:
I/ARMAssembler( 59): generated scanline__00000177:03515104_00001001_00000000 [
91 ipp] (114 ins) at [0x334348:0x334510] in 1430659 ns
W/System.err( 272): java.net.UnknownHostException: www.example.com
W/System.err( 272): at java.net.InetAddress.lookupHostByName(InetAddress.jav
a:513)
W/System.err( 272): at java.net.InetAddress.getAllByNameImpl(InetAddress.jav
a:278)
W/System.err( 272): at java.net.InetAddress.getAllByName(InetAddress.java:24
2)
W/System.err( 272): at org.apache.http.impl.conn.DefaultClientConnectionOper
ator.openConnection(DefaultClientConnectionOperator.java:136)
W/System.err( 272): at org.apache.http.impl.conn.AbstractPoolEntry.open(Abst
ractPoolEntry.java:164)
W/System.err( 272): at org.apache.http.impl.conn.AbstractPooledConnAdapter.o
pen(AbstractPooledConnAdapter.java:119)
W/System.err( 272): at org.apache.http.impl.client.DefaultRequestDirector.ex
ecute(DefaultRequestDirector.java:348)
W/System.err( 272): at org.apache.http.impl.client.AbstractHttpClient.execut
e(AbstractHttpClient.java:555)
W/System.err( 272): at org.apache.http.impl.client.AbstractHttpClient.execut
e(AbstractHttpClient.java:487)
W/System.err( 272): at org.apache.http.impl.client.AbstractHttpClient.execut
e(AbstractHttpClient.java:465)
W/System.err( 272): at android.net.http.AndroidHttpClient.execute(AndroidHtt
pClient.java:243)
W/System.err( 272): at com.collaboapp.android.HomeActivity.onClick(HomeActiv
ity.java:152)
W/System.err( 272): at android.view.View.performClick(View.java:2408)
W/System.err( 272): at android.view.View$PerformClick.run(View.java:8816)
W/System.err( 272): at android.os.Handler.handleCallback(Handler.java:587)
W/System.err( 272): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 272): at android.os.Looper.loop(Looper.java:123)
W/System.err( 272): at android.app.ActivityThread.main(ActivityThread.java:4
627)
W/System.err( 272): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 272): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 272): at com.android.internal.os.ZygoteInit$MethodAndArgsCalle
r.run(ZygoteInit.java:868)
W/System.err( 272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.ja
va:626)
W/System.err( 272): at dalvik.system.NativeStart.main(Native Method)
E/class android.HomeActivity( 272): www.example.com
D/ThrottleService( 59): finally have imsi - retreiving data
D/ThrottleService( 59): onPollAlarm - roaming =false, read =0, written =0, new
total =0
D/SntpClient( 59): request time failed: java.net.SocketException: Address fami
ly not supported by protocol
我在这里做错了什么?我可能需要从Android模拟器配置以使其正常工作吗?
感谢您的帮助。
答案 0 :(得分:7)
我不清楚为什么这个课程没有像预期的那样工作,也许有人可以解释。这个帖子也讨论了这个问题,但也没有解释代码失败的原因:http://groups.google.de/group/android-developers/browse_thread/thread/cc59efb9475ac557/81116369f2c6bd7a?hl=de&lnk=gst&q=This+thread+forbids+HTTP+requests#81116369f2c6bd7a。
答案 1 :(得分:6)
您不能通过UI线程调用Web,因此您不会暂停应用程序的UI,此博客文章通过使用AndroidHttpClient的示例应用程序解释它:Official Android Dev Blog。
这是一个引用:
...这是一个糟糕的想法,AndroidHttpClient不允许自己从主线程启动。上面的代码将显示“此线程禁止HTTP请求”错误消息。如果你真的想用脚射击自己,请使用DefaultHttpClient。
因此,如果你真的想在UI线程上运行它(不好的想法,从个人经验和博客文章),那么使用DefaultHttpClient。
答案 2 :(得分:0)
你在AndroidManifest.xml中有使用权限吗?
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
答案 3 :(得分:0)
谢谢,我找到了this,我刚刚启用了StrictMode
public void onCreate() {
if (DEVELOPER_MODE) {
StrictMode.enableDefaults();
}
super.onCreate();
}