请求的描述在: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}
按照网页上说的,要如何发送数据才行呢?我这样发送的问题在那里呢?求大家关注和帮助,另外我上面速回的内容要怎么解析才能使用每一项的值呢?
谢谢大家了
图书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 下载