求助,HttpResponse response = http.execute(post);报错了,不会搞 android网络编程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{ HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(img_path); HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode()==200){ HttpEntity entity = httpResponse.getEntity(); byte[] data = EntityUtils.toByteArray(entity); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); handler.sendEmptyMessage(10001); } }catch(Exception e){ }这个是获取网络图片的列子,你可以把data换成你想要的数据,比如stream流等 new Thread(new Runnable() { @Override public void run() { //你的请求 } }).start(); 要么就在子线程中请求,要么加上下面这些代码 if (Build.VERSION.SDK_INT >= 11) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); } 那可以帮我看一下不,这样的例子public static String postRequest(String url,Map<String ,String> param)throws Exception{ HttpPost post = new HttpPost(url); //如果传递多个参数可以对传递额参数进行封装 List<NameValuePair> par = new ArrayList<NameValuePair>(); for(String key:param.keySet()){ //封装请求参数 par.add(new BasicNameValuePair(key, param.get(key))); } //设置请求参数 post.setEntity(new UrlEncodedFormEntity(par,"gbk")); //发送post请求 HttpClient http = new DefaultHttpClient(); try { HttpResponse response = http.execute(post); if(response.getStatusLine().getStatusCode()==200){ String result = EntityUtils.toString(response.getEntity()); Log.v("result", "post请求成功!"); Log.v("result", result); return result; } } catch (Exception e) { e.printStackTrace(); } //Log.v("result", "post请求失败!"); return null; }HttpResponse response = http.execute(post);执行到这里就报错了 http://www.linuxidc.com/Linux/2011-08/40654.htm 方法一、Thread + Handler 参数可用全局变量方法二、AsyncTask ,可以传入参数 listView的removeFooterView执行后,listView点击事件失效, android中的sqlite数据库主要是什么用途的, OutOfMemory 内存溢出 Android客户端和服务端通信,采用哪种方式比较好? intent 小问题 急 !!!关于ListView控件显示图片的问题 请教关于onTouchMove的问题 求助高手,怎么才能让电脑识别不到我的android手机 4.4.2上webview的选择上传的按钮不起作用了 Android 按钮 搜索时多个ListFragment之间replace时,出现滞后现象, 给大家推荐一个新广告平台
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(img_path);
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity entity = httpResponse.getEntity();
byte[] data = EntityUtils.toByteArray(entity);
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
handler.sendEmptyMessage(10001);
}
}catch(Exception e){
}这个是获取网络图片的列子,你可以把data换成你想要的数据,比如stream流等
new Thread(new Runnable() { @Override
public void run() {
//你的请求
}
}).start();
if (Build.VERSION.SDK_INT >= 11) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
}
那可以帮我看一下不,这样的例子
public static String postRequest(String url,Map<String ,String> param)throws Exception{
HttpPost post = new HttpPost(url);
//如果传递多个参数可以对传递额参数进行封装
List<NameValuePair> par = new ArrayList<NameValuePair>();
for(String key:param.keySet()){
//封装请求参数
par.add(new BasicNameValuePair(key, param.get(key)));
}
//设置请求参数
post.setEntity(new UrlEncodedFormEntity(par,"gbk"));
//发送post请求
HttpClient http = new DefaultHttpClient();
try {
HttpResponse response = http.execute(post);
if(response.getStatusLine().getStatusCode()==200){
String result = EntityUtils.toString(response.getEntity());
Log.v("result", "post请求成功!");
Log.v("result", result);
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
//Log.v("result", "post请求失败!");
return null;
}HttpResponse response = http.execute(post);执行到这里就报错了
方法二、AsyncTask ,可以传入参数