如何在Android中发布数据?
我找到了使用http的PostData主题。但它不起作用 怎么了 我还在如何在Android中发布数据?,android,http,postdata,Android,Http,Postdata,我找到了使用http的PostData主题。但它不起作用 怎么了 我还在清单中请求了Android.permission.INTERNET public void postData() { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http:/mysite.com/postTest.php"); try { List<N
清单中请求了Android.permission.INTERNET
public void postData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:/mysite.com/postTest.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("pw", "12456"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
tv.setText(response.toString());
} catch (ClientProtocolException e) {
//
} catch (IOException e) {
//
}
}
public void postData(){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(“http:/mysite.com/postest.php”);
试一试{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“pw”,“12456”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
tv.setText(response.toString());
}捕获(客户端协议例外e){
//
}捕获(IOE异常){
//
}
}
编辑
我的申请将被终止,并显示以下消息:
不幸的是,PostDataProject已停止。
尝试使用EntityUtils获取响应
尝试使用EntityUtils
试试这个:
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = responce.getEntity();
// convert stream to String
BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder sb1 = new StringBuilder();
String line = null;
while ((line = buf.readLine()) != null) {
sb1.append(line);
}
tv.setText(sb1.toString());
如果数据仅用于textvew或small,则u可以直接设置为textview:
tv.setText(EntityUtils.toString(response.getEntity()));
并使用以下命令:
HttpPost httppost = new HttpPost("http://mysite.com/postTest.php");
而不是
HttpPost httppost = new HttpPost("http:/mysite.com/postTest.php");
试试这个:
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = responce.getEntity();
// convert stream to String
BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder sb1 = new StringBuilder();
String line = null;
while ((line = buf.readLine()) != null) {
sb1.append(line);
}
tv.setText(sb1.toString());
如果数据仅用于textvew或small,则u可以直接设置为textview:
tv.setText(EntityUtils.toString(response.getEntity()));
并使用以下命令:
HttpPost httppost = new HttpPost("http://mysite.com/postTest.php");
而不是
HttpPost httppost = new HttpPost("http:/mysite.com/postTest.php");
“它不起作用”-没有例外,没有描述,不知道问题是什么。我的问题被编辑了。感谢拥有一个完整的堆栈跟踪。问题已解决。这是一个愚蠢的错误。抱歉:)“它不工作”-没有例外,没有描述,不知道问题是什么。我的问题已编辑。感谢拥有一个完整的堆栈跟踪。问题已解决。这是一个愚蠢的错误。抱歉:)我跟踪了代码。此行中出现错误:HttpResponse response=httpclient.execute(httppost)代码>您缺少//在http:/I跟踪代码之后。此行中出现错误:HttpResponse response=httpclient.execute(httppost)代码>您缺少//在http之后:/