关于android HttpClient发送post的问题 发送http请求不能写在activity的主线程里,需要另外开个线程来实现这个功能 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 本来需要重新启动一个activity,不过你可以在super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);后面加入以下代码,就可以在一个activty访问网络了。 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .penaltyLog() .penaltyDeath() .build()); public class MainActivity extends Activity { private Thread mThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mThread = new Thread(runnable); mThread.start();//线程启动 } Runnable runnable = new Runnable() { @Override public void run() {//run()在新的线程中运行 HttpClient hc = new DefaultHttpClient(); HttpPost post=new HttpPost("http://carebaby.duapp.com/system/receivemessage.php"); List<NameValuePair>params=new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", "username")); params.add(new BasicNameValuePair("y", "x")); params.add(new BasicNameValuePair("x", "y")); try { HttpResponse hr = hc.execute(post); Toast.makeText(getApplicationContext(), hr.getEntity().getContent().toString(), Toast.LENGTH_SHORT).show(); } catch(Exception ex){ Log.i("http", "1"+ex.getMessage()); } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}像这样开一个线程怎么还是不行呐, 你在manifest里面声明了internet权限了么?另外,你的代码里面,你的params并没有添加到post请求对象里面。 第一,你的List<NameValuePair>params根本就没有使用啊,应该在HttpResponse hr = hc.execute(post); 之前加上这两句:httpEntity = new UrlEncodedFormEntity(params, "UTF-8"); httpPost.setEntity(httpEntity);第二,不能在新线程中直接Toast的,得使用Handler将数据发回到主线程。第三,建议楼主将target改为17以上,这样当你在主线程中直接访问网络的时候会直接报NetworkInMainThreadException。 tryRunnable runnable = new Runnable() { @Override public void run() {// run()在新的线程中运行 HttpClient hc = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://carebaby.duapp.com/system/receivemessage.php"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", "username")); params.add(new BasicNameValuePair("y", "x")); params.add(new BasicNameValuePair("x", "y")); try { post.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpResponse hr = hc.execute(post); InputStream is = hr.getEntity().getContent(); StringBuilder sb = new StringBuilder(); BufferedReader buffer = new BufferedReader( new InputStreamReader(is)); String line = null; while ((line = buffer.readLine()) != null) sb.append(line); Looper.prepare(); Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_SHORT).show(); Looper.loop(); } catch (Exception ex) { Log.i("http", "1" + ex.getMessage()); } } }; 求RGB565的byte数组 组成图片的java代码或算法 CallLog.Calls.DATE格式化问题 android绘图时重绘的问题 请教:XT800播放在线音乐问题 求mpeg2解码移植到android的方法 SQLite 请教一下win下msysgit 怎么安装repo? Android 中怎样才能让ZoomControls 竖直放置 2.3与4.0差别 android彩信MMS 怎么能把pdu打包好的 smil和附件分开 我只想要附件部分的pdu android4.4 编译问题 android zxing二维码扫描灵敏度问题
setContentView(R.layout.activity_main);后面加入以下代码,就可以在一个activty访问网络了。 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
private Thread mThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mThread = new Thread(runnable);
mThread.start();//线程启动
}
Runnable runnable = new Runnable() {
@Override
public void run() {//run()在新的线程中运行
HttpClient hc = new DefaultHttpClient();
HttpPost post=new HttpPost("http://carebaby.duapp.com/system/receivemessage.php");
List<NameValuePair>params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "username"));
params.add(new BasicNameValuePair("y", "x"));
params.add(new BasicNameValuePair("x", "y"));
try {
HttpResponse hr = hc.execute(post);
Toast.makeText(getApplicationContext(), hr.getEntity().getContent().toString(),
Toast.LENGTH_SHORT).show();
}
catch(Exception ex){
Log.i("http", "1"+ex.getMessage());
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}像这样开一个线程怎么还是不行呐,
另外,你的代码里面,你的params并没有添加到post请求对象里面。
第一,你的List<NameValuePair>params根本就没有使用啊,应该在HttpResponse hr = hc.execute(post); 之前加上这两句:httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(httpEntity);
第二,不能在新线程中直接Toast的,得使用Handler将数据发回到主线程。
第三,建议楼主将target改为17以上,这样当你在主线程中直接访问网络的时候会直接报NetworkInMainThreadException。
public void run() {// run()在新的线程中运行
HttpClient hc = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://carebaby.duapp.com/system/receivemessage.php");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "username"));
params.add(new BasicNameValuePair("y", "x"));
params.add(new BasicNameValuePair("x", "y"));
try {
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse hr = hc.execute(post);
InputStream is = hr.getEntity().getContent();
StringBuilder sb = new StringBuilder();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(is));
String line = null;
while ((line = buffer.readLine()) != null)
sb.append(line);
Looper.prepare();
Toast.makeText(getApplicationContext(), sb.toString(),
Toast.LENGTH_SHORT).show();
Looper.loop();
} catch (Exception ex) {
Log.i("http", "1" + ex.getMessage());
}
} };