Android 使用put请求
我正在尝试httpPut方法。我不知道怎么做。我正在试这个样品 在日志中我得到了很长的跟踪。。我能知道这句话是什么意思吗 Http响应:org.apache.Http.message。BasicHttpResponse@5273e44c 这意味着什么 这是我的密码Android 使用put请求,android,http-put,Android,Http Put,我正在尝试httpPut方法。我不知道怎么做。我正在试这个样品 在日志中我得到了很长的跟踪。。我能知道这句话是什么意思吗 Http响应:org.apache.Http.message。BasicHttpResponse@5273e44c 这意味着什么 这是我的密码 public class AndroidHTTPRequestsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)
public class AndroidHTTPRequestsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut=new HttpPut("http://www.google.com");
// Creating HTTP Post
// HttpPost httpPost = new HttpPost("http://www.example.com/login");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("message",
"Hi, trying Android HTTP post!"));
// Url Encoding the POST parameters
try {
httpPut.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
// e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPut);
// writing response to log
System.out.println("Http Response:" +response.toString());
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
// e.printStackTrace();
} catch (IOException e) {
// writing exception to log
// e.printStackTrace();
}
}
}
公共类AndroidHTTPRequestsActivity扩展了活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建HTTP客户端
HttpClient HttpClient=新的DefaultHttpClient();
HttpPut HttpPut=新HttpPut(“http://www.google.com");
//创建HTTP Post
//HttpPost HttpPost=新的HttpPost(“http://www.example.com/login");
//建筑柱参数
//键和值对
List nameValuePair=新的ArrayList(2);
添加(新的BasicNameValuePair(“电子邮件”)user@gmail.com"));
添加(新的BasicNameValuePair(“消息”),
“嗨,正在尝试Android HTTP post!”);
//对POST参数进行Url编码
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePair));
}捕获(不支持的编码异常e){
//将错误写入日志
//e.printStackTrace();
}
//发出HTTP请求
试一试{
HttpResponse response=httpClient.execute(httpPut);
//将响应写入日志
System.out.println(“Http响应:+Response.toString());
Log.d(“Http响应:,Response.toString());
}捕获(客户端协议例外e){
//将异常写入日志
//e.printStackTrace();
}捕获(IOE异常){
//将异常写入日志
//e.printStackTrace();
}
}
}
引用类的对象
BasicHttpResponse
请尝试以下操作以获得geeting响应:
HttpEntity httpEntity = null;
httpEntity = response.getEntity();
response = EntityUtils.toString(httpEntity);
代码是否正常工作?完整堆栈跟踪在哪里?此问题与作者先前的问题重复:。
HttpEntity httpEntity = null;
httpEntity = response.getEntity();
response = EntityUtils.toString(httpEntity);