这是我从Web服务获取列表视图数据的代码,我的代码工作正常并显示图像列表视图列表。但我想传递字符串customer_id以及Web服务调用,我想要的是只有在将customer_id传递给Web服务URL时才能在我的设备中获取Web服务数据,如注册过程。但是在这里我不知道如何传递字符串值,请跟我一起帮助我。
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "rank";
static String COUNTRY = "country";
static String POPULATION = "population";
static String FLAG = "flag";
TextView userid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
userid=(TextView) findViewById(R.id.customerid);
userid.setText("10");
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// private void feedback() {
// String customer_id = userid.getText().toString().trim().toLowerCase();
// list(customer_id);
// }
// private void list(String customer_id) {
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://sridharweb2.esy.es/gift_voucher.php" );
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("gift_voucher");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
// map.put("rank", jsonobject.getString("rank"));
map.put("country", jsonobject.getString("gift_voucher_name"));
map.put("population", jsonobject.getString("gift_voucher_price"));
map.put("flag", jsonobject.getString("gift_voucher_image"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
public void execute(String customer_id) {
}
}
// DownloadJSON ru = new DownloadJSON();
// ru.execute(customer_id);
// }
}
答案 0 :(得分:1)
您可以将客户ID传递给URL并执行这样的Web服务 -
HttpURLConnection connection = null;
URL url = new URL("http://sridharweb2.esy.es/gift_voucher.php");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setConnectTimeout(720000000);
connection.setReadTimeout(72000000);
connection.connect();
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("customer_id", customer_id)
String data = builder.build().getEncodedQuery();
OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
output.write(data);
output.flush();
output.close();
connection.disconnect();
在此基本上,您将附加所有输入数据并形成查询。
希望这会有所帮助!!