我使用购物车处理程序构建android 4.0应用程序存储在data / data /.../ cartlist.db中的一堆选择,并且需要知道如何将游标结果与List NameValuePair相关联, 这是我的活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendorder);
postData();
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
String[] columns = new String[] { "_id","name",
"qty", "price" };
Cursor messagesCursor= getContentResolver().query(
MyContentProvider.CONTENT_URI, columns, null, null,
null);
int theTotal = messagesCursor.getCount();
Toast.makeText(this, "Total6: " + theTotal, Toast.LENGTH_LONG).show();
int idCol = messagesCursor.getColumnIndex("_id");
int nameCol = messagesCursor.getColumnIndex("name");
int qtyCol = messagesCursor.getColumnIndex("qty");
int priceCol = messagesCursor.getColumnIndex("price");
while (messagesCursor.moveToNext()) {
columns[0] = Integer.toString((messagesCursor.getInt(idCol)));
quantity = messagesCursor.getString(nameCol);
product = messagesCursor.getString(qtyCol);
cost = messagesCursor.getString(priceCol);
}
}
public void postData() {
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost( "http://example.com/droid/order.php");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("qty", quantity));
nameValuePair.add(new BasicNameValuePair("product",product));
nameValuePair.add(new BasicNameValuePair("price",cost));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
答案 0 :(得分:0)
我发现只是错误地放置了名称值对并清理了一些大括号希望答案帮助一些人
public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.sendorder);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
String[] columns = new String[] { "_id","name",
"qty", "price" };
Cursor messagesCursor= getContentResolver().query(
MyContentProvider.CONTENT_URI, columns, null, null,
null);
int theTotal = messagesCursor.getCount();
Toast.makeText(this, "Total6: " + theTotal, Toast.LENGTH_LONG).show();
int idCol = messagesCursor.getColumnIndex("_id");
int nameCol = messagesCursor.getColumnIndex("name");
int qtyCol = messagesCursor.getColumnIndex("qty");
int priceCol = messagesCursor.getColumnIndex("price");
while (messagesCursor.moveToNext()) {
columns[0] = Integer.toString((messagesCursor.getInt(idCol)));
quantity = messagesCursor.getString(nameCol);
product = messagesCursor.getString(qtyCol);
cost = messagesCursor.getString(priceCol);
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("qty", quantity));
nameValuePair.add(new BasicNameValuePair("product",product));
nameValuePair.add(new BasicNameValuePair("price",cost));
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost( "http://example.com/droid/order.php");
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
}