首页 新闻 赞助 找找看

android java HttpPost请求怎么写

0
悬赏园豆:50 [待解决问题]

请求的描述在:http://appschallenge.juzz4.com/wp/?page_id=27

package jusi.singporecameratest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DefaultHttpClient httpClient
= new DefaultHttpClient();
String url
= "http://appschallenge.juzz4.com/api/login";
HttpPost httppost
= new HttpPost(url);
httppost.setHeader(
"Content-Type", "application/x-www-form-urlencoded");
List
<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(
new BasicNameValuePair("username", "demo"));
list.add(
new BasicNameValuePair("password", "demo"));
list.add(
new BasicNameValuePair("mechanism", "plain"));
String str
= null;

try {
httppost.setEntity(
new UrlEncodedFormEntity(list, HTTP.UTF_8));
HttpResponse response
= httpClient.execute(httppost);
str
= EntityUtils.toString(response.getEntity());
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 取得HTTP response
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



取回的response的内容是:{"reason":"Invalid input data. Please check your input data.","status":400}
按照网页上说的,要如何发送数据才行呢?我这样发送的问题在那里呢?求大家关注和帮助,另外我上面速回的内容要怎么解析才能使用每一项的值呢?
谢谢大家了

问题补充: 我发现用JSON了,换了这个方法还是同样的问题 package jusi.singporecameratest; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Test extends Activity { private TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String url = "http://appschallenge.juzz4.com/api/login"; HttpPost request = new HttpPost(url); request.setHeader("Content-Type","application/x-www-form-urlencoded"); // 先封装一个 JSON 对象 JSONObject param = new JSONObject(); try{ param.put("username", "demo"); param.put("password", "demo"); param.put("mechanism", "plain"); // 绑定到请求 Entry StringEntity se = new StringEntity(param.toString()); request.setEntity(se); HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 得到应答的字符串,这也是一个 JSON 格式保存的数据 String retSrc = EntityUtils.toString(httpResponse.getEntity()); // 生成 JSON 对象 JSONObject result = new JSONObject(retSrc); String token = (String) result.get("reason"); TextView tv = new TextView(this); tv.setText(token); setContentView(tv);} catch(Exception e){ } } }
jusi的主页 jusi | 初学一级 | 园豆:150
提问于:2011-03-07 21:03
< >
分享
所有回答(1)
0

图书Android in action的第六章有一个详细的例子,http://www.manning.com/ableson2/AndroidInAction_SourceCode.zip 这个是图书的例子代码,包括了HTTP请求,REST,XML解析等方面

图书可以在http://www.cnblogs.com/MaxWoods/archive/2011/01/19/1939177.html 下载

2012 | 园豆:21218 (高人七级) | 2011-03-07 21:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册