我正在尝试制作一个“Freebusy”请求以连接到Google Calendar API。目前我仍然坚持格式化http POST。我收到一个错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
我试图像这样格式化请求:
{
"timeMin": datetime,
"timeMax": datetime,
"timeZone": string,
"groupExpansionMax": integer,
"calendarExpansionMax": integer,
"items": [
{
"id": string
}
]
}
目前我正在这样做以格式化它:
String[] stringPairs = new String[]{
"timeMin", date1,
"timeMax", date2,
"items[]", calendarID,
"timezone", "Canada/Toronto"};
//Create an HTTP post request
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("HostULR");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(stringPairs.length/2 - 1);
for (int i = 0; i < stringPairs.length; i += 2) {
nameValuePairs.add(new BasicNameValuePair(stringPairs[i], stringPairs[i+1]));
}
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
org.apache.http.HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
我相信我正在搞砸的部分是"items"
部分。
任何帮助将不胜感激。
答案 0 :(得分:0)
不知道这对于java是否正确,但在C中你只是将另一个json插入其中以制作一个复杂的json。
也许这个网站可以帮到你。
编辑:我注意到表单是一个对象数组......所以正确的实现就是这样。
JSONObject obj=new JSONObject();
obj.put("ID", string);
JSONArray list = new JSONArray();
list.add(obj);
JSONObject jsonObj = new JSONObject();{
object.put("timeMin", date1)
....
另外,我不知道这段代码......
"timezone", "Canada/Toronto"}, accessToken );
对于json对象,它通常是{}符号json对象[]这些是数组。
编辑2: 创建JSON后执行此操作
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setHeader("Accept", "application/json");
request.setEntity(entity);
HttpResponse response =null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), Constants.ANDROID_CONNECTION_TIMEOUT*1000);
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),Constants.ANDROID_CONNECTION_TIMEOUT*1000);
try{
response = httpClient.execute(request);
}
catch(SocketException se)
{
Log.e("SocketException", se+"");
throw se;
}
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
答案 1 :(得分:0)
如果要创建一个与您尝试发送的json匹配并在其上放置ArrayList<String>
的类,然后使用Gson之类的Json解析器并使用StringEntity
进行发送帖子请求,这是一个例子:
public class JsonTest extends AsyncTask<GoogleCalendarJson, Void, String> {
private HttpClient client;
private HttpPost request;
private HttpResponse response;
private Activity activity;
public JsonTest(Activity a){
activity = a;
}
@Override
public String doInBackground(GoogleCalendarJson... object){
client = new DefaultHttpClient();
request = new HttpPost("YOUR URL HERE");
StringBuffer b = new StringBuffer("");
String json = new Gson().toJson(object[0]);
InputStream is = null;
BufferedReader br = null;
try{
StringEntity se = new StringEntity(json);
request.setHeader(HTTP.CONTENT_TYPE, "application/json");
request.setEntity(se);
response = client.execute(request);
is = response.getEntity().getContent();
br = new BufferedReader(new InputStreamReader(is));
String line = null;
while(((line = br.readLine()) != null)){
b.append(line);
}
br.close();
}catch(IOException e){
b.append(e.getMessage());
}
return b.toString();
}
@Override
public void onPostExecute(final String result){
activity.runOnUiThread(new Runnable(){
@Override
public void run(){
TextView t = (TextView)activity.findViewById(R.id.result);
t.setText(result);
}
});
}
}
configuration
是plain old java object
,它没有引用另一个configuration
对象,configuration
可能有一个ArrayList<String>
,我不确定如果你可以有一个ArrayList<Item>
你可以试试,作为一个加号,你可以使用HttpURLConnection而不是apache的HttpClient如果你正在为后姜饼应用编程这里是{{1 }} http://android-developers.blogspot.com/2011/09/androids-http-clients.html,希望这对任何人都有帮助。
PD:我没有在下面的代码中使用IDE,抱歉如果出现错误(我目前还没有Android Studio @home,我用netbeans格式化了这段代码:p)。
编辑:只是为了澄清,这个AsyncTask应该在UI线程中运行(比如在一个Activity中),如下所示:
WHY
编辑2:新代码实际上工作得非常完美,让我举一个GoogleCalendarJson类的例子:
GoogleCalendarJson类(必须有getter / setter):
JsonTest test = new JsonTest(this); //here we pass the Activity
test.execute(new GoogleCalendarJson()); //this is the object we get in 'object0]'
物品类(与g / s相同):
public class GoogleCalendarJson {
private String timeMin, timeMax, timezone;
private int groupExpanxionMaxm, calendarExpansionMax;
private ArrayList<Item> items;
public GoogleCalendarJson() {
}
}
使用Gson生成的Json示例:
public class Item {
private String id;
public Item() {
}
}
检查Json结构,你可以在上面粘贴json JsonValidator
要测试此代码,您需要一个至少包含ID为{"timezone":"someTimezone","timeMin":"min","items":
[{"id":"someID"}],"timeMax":"max","groupExpanxionMaxm":2,"calendarExpansionMax":1}