|
|
package com.example.PersonalCenter;
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.ProtocolException;
|
|
|
import java.net.URL;
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class SearchServiceGet {
|
|
|
|
|
|
public static ArrayList<String> content=new ArrayList<String>();//存储数据内容
|
|
|
public static String name_back;
|
|
|
public static boolean SearchSuccess=false;
|
|
|
public static int code;
|
|
|
|
|
|
public static void executeSearchGet(String name){
|
|
|
HttpURLConnection connection = null;
|
|
|
InputStream in = null;
|
|
|
|
|
|
String address="/rest/cmkg/question/hello";
|
|
|
|
|
|
String Url = "http://114.116.199.154:5000/cmkg"+address;
|
|
|
String path = Url + "?name="+name;
|
|
|
URL url = null;
|
|
|
try {
|
|
|
url = new URL(path);
|
|
|
connection = (HttpURLConnection)url.openConnection();
|
|
|
connection.setRequestMethod("GET");
|
|
|
connection.setConnectTimeout(100000);//建立连接超时
|
|
|
connection.setReadTimeout(80000);//传输数据超时
|
|
|
in = connection.getInputStream();
|
|
|
|
|
|
BufferedReader reader = null;//输入流
|
|
|
String line = "";//读取返回的每一行
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
try {
|
|
|
reader = new BufferedReader(new InputStreamReader(in));
|
|
|
while((line = reader.readLine()) != null) {
|
|
|
response.append(line);
|
|
|
}
|
|
|
//这时候response就是一个连续字符串了吧
|
|
|
Log.i("response+++++",response+"返回值");
|
|
|
System.out.println(response.toString());
|
|
|
|
|
|
//开始解析返回的数据
|
|
|
// JSONObject jo=JSONObject.fromObject(response.toString());
|
|
|
org.json.JSONObject jo = null;
|
|
|
try {
|
|
|
jo = new org.json.JSONObject(response.toString());
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
try {
|
|
|
code=jo.getInt("code");
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
System.out.println("code="+code);
|
|
|
if(code!=0){ //查询成功,在开始解析数据
|
|
|
System.out.println("code==@@@@@@@@@@20"+code);
|
|
|
try {
|
|
|
name_back=jo.getString("message");//查询的主语
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
//把data看作一个JSONObject,无论里面由多少元素,都转换为JSONArray,存在数组里
|
|
|
JSONObject jo_data= null;
|
|
|
|
|
|
try {
|
|
|
jo_data = jo.getJSONObject("data");
|
|
|
if(jo_data==null) System.out.println(("jo_data==null"));
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
JSONArray data_arr=new JSONArray();
|
|
|
ArrayList<String> datas=new ArrayList<String>();
|
|
|
try {
|
|
|
|
|
|
// jo_data.toJSONArray(data_arr);
|
|
|
datas.add(jo_data.toString());
|
|
|
data_arr=jo.getJSONArray("data");
|
|
|
System.out.println("data_arr======"+data_arr.length());
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
//遍历data_arr,把里面的数据放入ArrayList<String> content里
|
|
|
for(int i=0;i<datas.size();i++){
|
|
|
try {
|
|
|
content.add(datas.get(i));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
SearchSuccess=true;//查找成功标志
|
|
|
}
|
|
|
// return content;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
|
if(reader!=null){
|
|
|
try {
|
|
|
reader.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (MalformedURLException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (ProtocolException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
// return "";
|
|
|
}
|
|
|
|
|
|
public static void exxcuteTest(String name){
|
|
|
code=100;
|
|
|
name_back="糖尿病";
|
|
|
content.add("莲子粥");
|
|
|
content.add("推拿法");
|
|
|
content.add("大蒜蒸西瓜");
|
|
|
content.add("红枣糕");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|