HttpUtilNew:
package com.fooddemo;
import java.io.IOException;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpUtilNew {
// 提交请求
public static HttpClient client= new DefaultHttpClient();
// getRequest(String url,Map<String, String> params)
public static String getRequest(String url,Map<String, String> params){
StringBuffer sb=new StringBuffer();
String result="";
// sb.append("http://apis.juhe.cn/catering/query?key=f5a1f5d1efd83cf0026ec5912c5f9208&lng=121.538123&lat=31.677132&radius=20");
sb.append("http://apis.juhe.cn/catering/");
sb.append(url);
// 拼接参数
if(params != null){
sb.append("?");
for(Map.Entry<String, String> entry:params.entrySet()){
String key=entry.getKey();
String value=entry.getValue();
sb.append(key + "=" + value + "&");
}
}
// 删除最后的"&"
sb.delete(sb.length() - 1, sb.length());
// // 发送GET请求
HttpGet get= new HttpGet(sb.toString());
try {
HttpResponse response = client.execute(get);
HttpEntity entity=response.getEntity();
result=EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println("result---"+result);
return result;
}
}
MyHttpClient:
package com.fooddemo;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
public class MyHttpClient {
private static HttpClient mHttpClient;
public static synchronized HttpClient getHttpClient() {
if (mHttpClient == null) {
HttpParams params = new BasicHttpParams();
ConnManagerParams.setTimeout(params, 5000);
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 10000);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schReg);
mHttpClient = (HttpClient) new DefaultHttpClient(conMgr, params);
}
return mHttpClient;
}
}
JsonActivity:
package com.acyivity.Utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import com.activity.bean.Dish;
import com.activity.bean.Table;
import com.activity.bean.User;
public class JsonActivity {
/**
*判断是否申请成功返回的字符串
*
* @param str
* @return
* @throws JSONException
*/
public static Map<String, String> getJsonLogin(String str)
throws JSONException {
JSONObject obj = new JSONObject(str);
String rt = obj.getString("rt");
String rtmsg = obj.getString("rtmsg");
Map<String, String> map = new HashMap<String, String>();
map.put("rt", rt);
map.put("rtmsg", rtmsg);
return map;
}
/**
* 解析菜单
*
* @param str
* @return
* @throws JSONException
*/
public static List<Dish> getDish(String str) throws JSONException {
List<Dish> arr = new ArrayList<Dish>();
Dish d = null;
JSONObject obj = new JSONObject(str);
JSONArray list = obj.getJSONArray("list");
for (int i = 0; i < list.length(); i++) {
JSONArray jo = list.getJSONArray(i);
String dish_id = jo.getString(0);// 编号
String dish_class = jo.getString(1);// 菜类
String diah_name = jo.getString(2);// 菜名
String image = jo.getString(3);// 图片位置
int price = jo.getInt(4);// 价格
String itrInfo = jo.getString(5);// 介绍
int dish_num = jo.getInt(6);// 数量
d = new Dish();
d.setDish_id(dish_id);
d.setDish_class(dish_class);
d.setDiah_name(diah_name);
d.setImage(image);
d.setPrice(price);
d.setItrInfo(itrInfo);
d.setDish_num(dish_num);
arr.add(d);
// Log.i("arr", arr+"");
}
return arr;
}
/**
* 解析桌子
*
* @param str
* @return
* @throws JSONException
*/
public static List<Table> getTable(String str) throws JSONException {
List<Table> listTable = new ArrayList<Table>();
Table table = null;
JSONObject obj = new JSONObject(str);
JSONArray list = obj.getJSONArray("list");
for (int i = 0; i < list.length(); i++) {
JSONArray jo = list.getJSONArray(i);
String tableNum = jo.getString(0);// 桌号
String adress = jo.getString(1);// 位置
String peopleNum = jo.getString(2);// 人数
String tableClass = jo.getString(3);// 桌子类型
table = new Table();
table.setTable_num(tableNum);
table.setAddress(adress);
table.setSize(peopleNum);
table.setTable_name(tableClass);
listTable.add(table);
}
return listTable;
}
public static List<String> getJsonOeder(String str) throws JSONException {
List<String> p = new ArrayList<String>();
JSONObject js = new JSONObject(str);
JSONArray ja = js.getJSONArray("list");
for (int x = 0; x < ja.length(); x++) {
JSONArray j = ja.getJSONArray(x);
String orederID = j.getString(0);
p.add(orederID);
}
return p;
}
/**
* 注册
*
* @param userList
* @return
*/
public static Map<String, String> getRegiste(User user) {
List<String> list = new ArrayList<String>();
list.add(user.getUser_name());
list.add(user.getUser_pwd());
list.add(user.getUser_sex());
list.add(user.getUser_birthday());
list.add(user.getUser_phone());
Map<String, String> resultMap = null;
try {
JSONArray ja = new JSONArray(list);
// 配置参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("list", ja.toString()));
// 提交注册,接收返回信息
String str = HttpUtil.postRequest("registerServlet", params);
resultMap = getJsonLogin(str);
Log.i("resultMap", resultMap + "");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return resultMap;
}
}
.XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="半径:" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp"
android:text="TextView" />
</RelativeLayout>