您的位置:首页 > Web前端 > JavaScript

post请求和json解析

2016-02-26 11:10 579 查看
package com.xueshen.Analytical;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.xueshen.Data.book;

public class Analytical {

/**
* @param qian
*/
private static final String SERVER_URL = "http://api.yi18.net/book/list";

public List<book> get(String boos) throws Exception{
// 发送http请求
String request = String.format(SERVER_URL, boos);
System.out.println(request);
URI uri = new URI(request);
HttpPost httpPost = new HttpPost(uri);
System.out.println(httpPost);

HttpResponse response = new DefaultHttpClient().execute(httpPost);
String str = EntityUtils.toString(response.getEntity());
//System.out.println(response);
System.out.println(str);
return parseWeather(str);

}
private List<book> parseWeather(String str) throws Exception{
/*JSONObject jsonObj = new JSONObject(str);
JSONArray jsonArray1 = jsonObj.getJSONArray("yi18");

List<book> weatherDataList = new ArrayList<book>();
for(int i=0;i<jsonArray1.length();i++){
JSONObject jsonWeath = jsonArray1.getJSONObject(i);

book w = new book();
w.setName(jsonWeath.getString("name"));
w.setImg(jsonWeath.getString("img"));
w.setFrom(jsonWeath.getString("from"));
w.setAuthor(jsonWeath.getString("author"));
w.setSummary(jsonWeath.getString("summary"));
w.setBookclass(jsonWeath.getString("bookclass"));
w.setCount(jsonWeath.getString("count"));
w.setFcount(jsonWeath.getString("fcount"));
w.setRcount(jsonWeath.getString("rcount"));
w.setId(jsonWeath.getString("id"));

weatherDataList.add(w);

// System.out.println(w);
//Log.d(tag,jsonWeath.toString());
}

//System.out.println(String.valueOf(weatherDataList.size()));
return weatherDataList;
}*/
return null;
}
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: