android访问php取回json数据,从PHP json中提取数据以在android中使用

我正在使用此代码从PHP文件中提取一些数据并将一些数据提取到我的android应用程序中。 此代码提取每个产品的名称,价格和可用性,并将每个产品放在一个字符串中。 现在我需要在java中包含产品的数组,其中包含每个产品的名称,价格和可用性,并将它们命名为product1 product2 product3,以便我可以基于此编写其余代码。 我该怎么做?从PHP json中提取数据以在android中使用

public class MainActivity extends Activity {

private String jsonResult;

private String url = "xxxx/get_all_products.php";

private ListView listView;

private static final String TAG_PRODUCTS = "products";

private static final String TAG_PID = "pid";

private static final String TAG_NAME = "name";

private static final String TAG_PRICE = "price";

private static final String TAG_FOUND = "found";

private static final String TAG_DESCRIPTION = "description";

ArrayList> productList;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.listView1);

productList = new ArrayList>();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView> parent, View view,

int position, long id) {

String selval = ((TextView) view.findViewById(R.id.name)).getText().toString();

String selval1 = ((TextView) view.findViewById(R.id.price)).getText().toString();

String selval2 = ((TextView) view.findViewById(R.id.found)).getText().toString();

// Also I've found a solution on SO that a guy solved this problem doing soemthing like this :

// TextView txt = (TextView) parent.getChildAt(position - listview.firstVisiblePosition()).findViewById(R.id.sometextview);

// String keyword = txt.getText().toString();

Intent intnt = new Intent(getApplicationContext(), SingleListItem.class);

intnt.putExtra("selval", selval);

intnt.putExtra("selval1", selval1);

intnt.putExtra("selval2", selval2);

startActivity(intnt);

}

});

accessWebService();

}

// Async Task to access the web

private class JsonReadTask extends AsyncTask {

@Override

protected String doInBackground(String... params) {

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(params[0]);

try {

HttpResponse response = httpclient.execute(httppost);

jsonResult = inputStreamToString(

response.getEntity().getContent()).toString();

}

catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

private StringBuilder inputStreamToString(InputStream is) {

String rLine = "";

StringBuilder answer = new StringBuilder();

BufferedReader rd = new BufferedReader(new InputStreamReader(is));

try {

while ((rLine = rd.readLine()) != null) {

answer.append(rLine);

}

}

catch (IOException e) {

// e.printStackTrace();

Toast.makeText(getApplicationContext(),

"Error..." + e.toString(), Toast.LENGTH_LONG).show();

}

return answer;

}

@Override

protected void onPostExecute(String result) {

ListDrwaer();

}

}// end async task

public void accessWebService() {

JsonReadTask task = new JsonReadTask();

// passes values for the urls string array

task.execute(new String[]{url});

}

// build hash set for list view

public void ListDrwaer() {

List> productList = new ArrayList>();

try {

JSONObject jsonResponse = new JSONObject(jsonResult);

JSONArray jsonMainNode = jsonResponse.optJSONArray("products");

for (int i = 0; i < jsonMainNode.length(); i++) {

JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

String name = jsonChildNode.optString("name");

String price = jsonChildNode.optString("price");

String found = jsonChildNode.optString("found");

// String outPut = name + "-" + number;

// String outPut = name + "-" + price + "-" + found;

// productList.add(createProduct("products", outPut));

HashMap product = new HashMap();

product.put(TAG_NAME, name);

product.put(TAG_FOUND, found);

product.put(TAG_PRICE, price);

productList.add(product);

}

} catch (JSONException e) {

Toast.makeText(getApplicationContext(), "Error" + e.toString(),

Toast.LENGTH_SHORT).show();

}

SimpleAdapter simpleAdapter = new SimpleAdapter(this, productList,

R.layout.list_item, new String[] { TAG_NAME, TAG_PRICE,

TAG_FOUND }, new int[] { R.id.name,

R.id.price, R.id.found });

listView.setAdapter(simpleAdapter);

}

}

2015-08-27

Mr.andi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值