我试图使用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);
}
答案 0 :(得分:1)
您可以使用排球库。 Android volley是一个网络库,用于使网络调用更容易,更快,而无需编写大量代码。默认情况下,所有排球网络调用都是异步工作的,因此我们不必担心再使用asynctask。
答案 1 :(得分:1)
首先,您应该在单独的线程中移动代码以避免ANR。您可以使用AsyncTask但要注意内存泄漏。 否则你可以按照建议或OkHttp使用截击。它们是帮助您管理HTTP连接并在单独的线程中处理请求的库。
例如,如果你想使用OkHttp:
-(void)saveObject:(NSObject *)object toFile:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:name];
[NSKeyedArchiver archiveRootObject:object toFile:appFile];
}
-(NSObject *)loadFromFile:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:name];
if([[NSFileManager defaultManager] fileExistsAtPath:appFile])
return [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
else{
NSLog(@"No File: %@", name) ;
return nil ;
}
}
希望它能帮到你!!
答案 2 :(得分:1)
首先,您应该知道在主UI上运行此代码会给您带来错误,您尝试在AsyncTask中实现此功能。
其次,&#34; JSONObject jsonResponse = new JSONObject(jsonResult);&#34; 你在哪里宣布&#34; jsonResult&#34;变量,根据你的代码,我不知道你宣布的地方。此外,您应该知道DefaultHttpClient现已弃用,最好将UrlConnect用于您的http调用。
最后我同意Patil的意见,你应该开始使用一些安卓网络库,例如Retrofit或Volley,并且因为你是初学者而玩。