[英]How can i post data to php using json just to receive data depending on posted data?
我正在嘗試使用json將數據發布到我的php文件中。 在我的php文件中,我想將發布的數據用於mySQL-Select-Statement並將選擇的內容返回到我的代碼中,並將其顯示為列表。
最簡單的方法是什么?
這是我的“ postData()”:
public void postData() {
//trying
List<Map<String, String>> buyCategories = new ArrayList<Map<String, String>>();
//trying
try{
// url where the data will be posted
String postReceiverUrl = "http://www.url.de/getArticles.php";
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
int cat = UsingSharedPrefs.getChosenCategorie();
if(cat == 0) {
cat = 100;
}
String cats = Integer.toString(cat);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("categorieId", cats));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.getJSONArray("testproducts");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.getString("testproduct_name");
String price = jsonChildNode.getString("testproduct_price");
String path = jsonChildNode.getString("testproduct_buffer");
HashMap<String, String> hmBuyCategories = new HashMap<String,String>();
hmBuyCategories.put("txt", "Categorie : " + name);
hmBuyCategories.put("pri", "Price : " + price);
hmBuyCategories.put("bes","Currency : " + path);
int imageId = getResources().getIdentifier("jollocks.logle:drawable/" + path, null, null);
hmBuyCategories.put("pic", Integer.toString(imageId) );
buyCategories.add(hmBuyCategories);
}
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Errortest" + e.toString(),
Toast.LENGTH_SHORT).show();
}
String[] from = { "pic","txt","pri", "bes" };
int[] to = { R.id.buffer,R.id.name, R.id.price, R.id.beschreibung};
ListView listView = ( ListView ) findViewById(R.id.sampleListView);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, buyCategories,
R.layout.list_item_product,
from, to);
listView.setAdapter(simpleAdapter);
}
您可以使用排球庫。 Android Volley是一個聯網庫,旨在使聯網呼叫變得更加輕松,快捷,而無需編寫大量代碼。 默認情況下,所有齊射網絡調用均異步工作,因此我們不必擔心再使用asynctask。
首先,您應該將代碼移到單獨的線程中以避免ANR。 您可以使用AsyncTask,但要注意內存泄漏。 否則,您可以按照建議使用Volley或OkHttp。 它們是可幫助您管理HTTP連接並在單獨的線程中處理請求的庫。
例如,如果您想使用OkHttp:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
// Notify error
}
@Override
public void onResponse(Response response) throws IOException {
// Here you parse the response
// when ready update the View
handler.post(new Runnable() {
@Override
public void run() {
// Here you update the listview
}
});
}
});
希望對你有幫助!!
首先,您應該知道在主UI上運行此代碼會給您帶來錯誤,您嘗試在AsyncTask中實現它。
其次,“ JSONObject jsonResponse =新的JSONObject(jsonResult);” 您在哪里聲明了“ jsonResult”變量,根據您的代碼,我看不到您聲明的位置。 您還應該知道DefaultHttpClient已被棄用,最好將UrlConnect用於您的http調用。
最后,我同意Patil的觀點,由於您是初學者,因此您應該接觸一些android聯網庫(例如Retrofit或Volley)並嘗試一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.