代码其实也不多,别人的要求就这样点,类不多。这个例子主要是学会自己搭建SSH进行后台处理、,前台就是简单的get请求。
使用json进行数据交付
遇到的问题就是编码问题......................
本例子所有图片 都是盗用别人的,,,,,自己真不会做图片
由于是帮别人做的项目,暂时不上传所有代码.....
BookJson.java
package com.book.json;
import java.util.List;
import com.book.model.Book;
public interface BookJson {
public List<String> Gettitles(String type);
public String GetContent(String title);
}
BookJsonimpl.java
package com.book.jsonimpl;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
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.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.book.json.BookJson;
import com.book.model.Book;
public class BookJsonImpl implements BookJson{
private static final String typetotitle="http://210.30.12.33:8080/Book/bookstbt.action";
private static final String titletocontent="http://210.30.12.33:8080/Book/bookcbt.action";
private HttpResponse response;
private String result;
@Override
public List<String> Gettitles(String type) {
// TODO Auto-generated method stub
List<String> strings=new ArrayList<String>();
HttpGet get=new HttpGet(typetotitle+"?typename="+URLEncoder.encode(type));
HttpClient client=new DefaultHttpClient();
try {
response=client.execute(get);//执行Post方法
result=EntityUtils.toString(response.getEntity());
JSONObject object=new JSONObject(result).getJSONObject("Titles");
for (int i = 0; i < object.length(); i++) {
strings.add(object.getString("title"+i));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return strings;
}
@Override
public String GetContent(String title) {
// TODO Auto-generated method stub
String strings=new String();
HttpGet get=new HttpGet(titletocontent+"?titlename="+URLEncoder.encode(title));
HttpClient client=new DefaultHttpClient();
try {
response=client.execute(get);//执行Post方法
result=EntityUtils.toString(response.getEntity());
JSONObject object=new JSONObject(result).getJSONObject("ContentS");
for (int i = 0; i < object.length(); i++) {
strings=object.getString("content"+i);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return strings;
}
}
以上是请求的代码..............展示一下借用了别人的 图片的效果