Java 如何将post值传递给服务器并将结果存储在listview中
这是我的listview,其中数据以JSON格式来自远程服务器,因此一切正常,但现在我必须将某个值传递给服务器,然后根据该值进行筛选,然后仅将所需结果加载到listview中Java 如何将post值传递给服务器并将结果存储在listview中,java,android,json,post,android-asynctask,Java,Android,Json,Post,Android Asynctask,这是我的listview,其中数据以JSON格式来自远程服务器,因此一切正常,但现在我必须将某个值传递给服务器,然后根据该值进行筛选,然后仅将所需结果加载到listview中 public class Reciepe extends AppCompatActivity { String Barname; TextView food,price; private ListView reciepeListView; private ProgressDialog loading; @Overri
public class Reciepe extends AppCompatActivity {
String Barname;
TextView food,price;
private ListView reciepeListView;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reciepe);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(Color.parseColor("#FFBC03"));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
new JSONTask().execute("http://thehostels.in/Foody/reciepe_json.php");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(Reciepe.this)
.defaultDisplayImageOptions(options)
.build();
com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
reciepeListView = (ListView)findViewById(R.id.list_recipe);
Intent intent=getIntent();
if(intent!=null){
Barname=intent.getStringExtra("Type");
Log.e("Type",Barname);
}
if (Barname != null) {
switch (Barname) {
case "Punjabi":
getSupportActionBar().setTitle("Punjabi");
break;
case "Chinese":
getSupportActionBar().setTitle("Chinese");
break;
case "South Indian":
getSupportActionBar().setTitle("South Indian");
break;
case "Gujarati":
getSupportActionBar().setTitle("Gujarati");
break;
case "Chicken":
getSupportActionBar().setTitle("Chicken");
break;
}
}
}
public class JSONTask extends AsyncTask<String, String, List<Listview_reciepe_conveyer>> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Reciepe.this, "loading,please wait...", null, true, true);
}
@Override
protected List<Listview_reciepe_conveyer> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("list");
List<Listview_reciepe_conveyer> fixture_conveyerList = new ArrayList<Listview_reciepe_conveyer>();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
Listview_reciepe_conveyer fixtureList = new Listview_reciepe_conveyer();
fixtureList.setImage(finalObject.getString("image"));
fixtureList.setFood(finalObject.getString("food"));
fixtureList.setPrice(finalObject.getString("price"));
fixture_conveyerList.add(fixtureList);
}
return fixture_conveyerList;
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<Listview_reciepe_conveyer> result) {
super.onPostExecute(result);
if (result !=null) {
loading.dismiss();
ListAdapter adapter = new ListAdapter(Reciepe.this, R.layout.custom_recipe_list, result);
reciepeListView.setAdapter(adapter);
}
else
{
Toast.makeText(Reciepe.this, "No Internet Connection!", Toast.LENGTH_LONG).show();
loading.dismiss();
}
}
}
public class ListAdapter extends ArrayAdapter {
private List<Listview_reciepe_conveyer> reciepe_conveyerList;
private int resource;
private LayoutInflater inflater;
public ListAdapter(Context context, int resource, List<Listview_reciepe_conveyer> objects) {
super(context, resource, objects);
reciepe_conveyerList = objects;
this.resource = resource;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(resource, null);
}
ImageView food_photo;
final TextView food,price;
food_photo = (ImageView)convertView.findViewById(R.id.food_photo);
food = (TextView)convertView.findViewById(R.id.food_name);
price = (TextView)convertView.findViewById(R.id.food_price);
ImageLoader.getInstance().displayImage(reciepe_conveyerList.get(position).getImage(), food_photo);
food.setText(reciepe_conveyerList.get(position).getFood());
String newprice= ("Rs."+reciepe_conveyerList.get(position).getPrice());
price.setText(newprice);
reciepeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(Reciepe.this,Description.class);
i.putExtra("Dish",reciepe_conveyerList.get(position).getFood());
i.putExtra("Price",reciepe_conveyerList.get(position).getPrice());
startActivity(i);
}
}
);
return convertView;
}
}
}
公共类Reciepe扩展了AppCompative活动{
字符串名称;
TextView食品、价格;
私有ListView ReceipeListView;
私有对话框加载;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receipe);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
toolbar.setBackgroundColor(Color.parseColor(“#FFBC03”);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
新建JSONTask()。执行(“http://thehostels.in/Foody/reciepe_json.php");
DisplayImageOptions=new DisplayImageOptions.Builder()
.cacheInMemory(真)
.cacheOnDisk(真)
.build();
ImageLoaderConfiguration配置=新建ImageLoaderConfiguration.Builder(Reciepe.this)
.defaultDisplayImageOptions(选项)
.build();
com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(配置);
ReceipeListView=(ListView)findViewById(R.id.list\u配方);
Intent=getIntent();
if(intent!=null){
Barname=intent.getStringExtra(“类型”);
Log.e(“Type”,Barname);
}
if(Barname!=null){
交换机(Barname){
“旁遮普”案:
getSupportActionBar().setTitle(“旁遮普”);
打破
案例“中文”:
getSupportActionBar().setTitle(“中文”);
打破
“南印度”案:
getSupportActionBar().setTitle(“南印度”);
打破
“古吉拉特邦”案:
getSupportActionBar().setTitle(“古吉拉特邦”);
打破
案例“鸡”:
getSupportActionBar().setTitle(“鸡肉”);
打破
}
}
}
公共类JSONTask扩展了异步任务{
对话加载;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loading=ProgressDialog.show(Reciepe.this,“正在加载,请稍候…”,null,true,true);
}
@凌驾
受保护列表doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(行);
}
字符串finalJson=buffer.toString();
JSONObject parentObject=新的JSONObject(finalJson);
JSONArray parentArray=parentObject.getJSONArray(“列表”);
List fixture_ConfererList=新建ArrayList();
对于(int i=0;iHttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("firstParam", "paremeterValue"));
//your param nr.1.
//This is the value that you want to send.
//It is represented like 'name=value', or in your case 'firstParam=parameterValue'.
//You need to edit this field in respect to what you are doing.
params.add(new BasicNameValuePair("secondParam", "your2parameter"));
//your param nr.2
//This is the value that you want to send.
//It is represented like 'name=value', or in your case 'secondParam=your2parameter'.
//You need to edit this field in respect to what you are doing.
params.add(new BasicNameValuePair("thirdParam", "anotherParameter"));
//your param nr.3
//This is the value that you want to send.
//It is represented like 'name=value', or in your case 'thirdParam=anotherParameter'.
//You need to edit this field in respect to what you are doing.
// Write(add) parameters to your request
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
private String getQuery(List<BasicNameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (String pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}