Nasıl JSON POST isteği HTTPClient kullanarak göndermek için?
HTTPClient kullanarak android JSON POST nasıl anlamaya çalışıyorum. Bu bir süre anlamaya çalışıyorum, örnekler, çevrimiçi bir sürü buldum, ama işe hiçbirini alamıyorum. Bu genel olarak JSON/ağ bilgisi benim eksikliği nedeniyle olduğuna inanıyorum. Orada örnekler bol ama biri gerçek bir öğretici beni işaret ediyor olabilir. Bu adım, yaptığı her adım için neden kod ve açıklama ile adım süreci bir adım için, ya da arıyorum. Karmaşık, basit bir kafi olmasına gerek yok.
Yine, orada örnek bir ton olduğunu biliyorum, sadece gerçekten ne olup bittiğini ve bu şekilde yapılır neden bir açıklama ile bir örnek arıyorum.
Eğer kimse bu fırsatlar iyi Android bir kitap bilen varsa lütfen bana bildirin.
Tekrar teşekkürler yardım için @terrance, aşağıda tarif ettim kodu
public void shNameVerParams() throws Exception{
String path = //removed
HashMap params = new HashMap();
params.put(new String("Name"), "Value");
params.put(new String("Name"), "Value");
try {
HttpClient.SendHttpPost(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CEVAP
Bu cevap example posted by Justin Grammens kullanıyorum.
Hakkında JSON
JSON JavaScript Nesne Gösterimi için duruyor. JavaScript özelliklerini bu gibi object1.name
böyle de başvurulabilir object['name'];
. Makalesinden örnek JSON bu bit kullanır.
Parçalar
Bir fan bir anahtar olarak e-posta ile nesne ve bir değer olarak foo@bar.com< / ^ br .
{
fan:
{
email : 'foo@bar.com'
}
}
Nesne eşdeğer fan.email;
fan['email'];
olur. Her ikisi de aynı değeri olurdu
'foo@bar.com'
.
Hakkında HttpClient İsteği
Aşağıdaki ne yazar HttpClient Request yapmak için kullanılır. Eğer kimseye bir kelime için daha iyi bir yol varsa terminoloji bazı çekinmeyin bu yüzden bu konuda uzman olduğumu iddia etmiyorum.
public static HttpResponse makeRequest(String path, Map params) throws Exception
{
//instantiates httpclient to make request
DefaultHttpClient httpclient = new DefaultHttpClient();
//url with the post data
HttpPost httpost = new HttpPost(path);
//convert parameters into JSON object
JSONObject holder = getJsonObjectFromMap(params);
//passes the results to a string builder/entity
StringEntity se = new StringEntity(holder.toString());
//sets the post request as the resulting string
httpost.setEntity(se);
//sets a request header so the page receving the request
//will know what to do with it
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
//Handles what is returned from the page
ResponseHandler responseHandler = new BasicResponseHandler();
return httpclient.execute(httpost, responseHandler);
}
Göster
Eğer Map
veri yapısı hakkında bilginiz varsa lütfen Java Map reference bir göz atın. Kısacası, bir harita, bir sözlük ya da bir hash benzer.
private static JSONObject getJsonObjectFromMap(Map params) throws JSONException {
//all the passed parameters from the post request
//iterator used to loop through all the parameters
//passed in the post request
Iterator iter = params.entrySet().iterator();
//Stores JSON
JSONObject holder = new JSONObject();
//using the earlier example your first entry would get email
//and the inner while would get the value which would be 'foo@bar.com'
//{ fan: { email : 'foo@bar.com' } }
//While there is another entry
while (iter.hasNext())
{
//gets an entry in the params
Map.Entry pairs = (Map.Entry)iter.next();
//creates a key for Map
String key = (String)pairs.getKey();
//Create a new map
Map m = (Map)pairs.getValue();
//object for storing Json
JSONObject data = new JSONObject();
//gets the value
Iterator iter2 = m.entrySet().iterator();
while (iter2.hasNext())
{
Map.Entry pairs2 = (Map.Entry)iter2.next();
data.put((String)pairs2.getKey(), (String)pairs2.getValue());
}
//puts email and 'foo@bar.com' together in map
holder.put(key, data);
}
return holder;
}
. ben^>
Çekinmeyin yorum herhangi bir soru ortaya ilgili bu yazı ya, elimde değil apaçık bir şey yoksa eğer ben dokunmadım bir şey senin hala kafası karışık... vs. ne olursa olsun babalık kafanı gerçekten.
(Eğer Justin Grammens onaylıyor mu yoksa öldürürüm. Ama eğer değilse o zaman dert etmediğin için teşekkürler Justin.)
Güncelleme
Ben sadece kodu kullanma hakkında bir yorum almak oldu ve dönüş türü bir hata olduğunu fark etti. Yöntem imzası bir dize döndürmek için ayarlandı ama bu durumda dönen bir şey değildi. İmzayı değiştirdim HttpResponse ve *17 Bu Bağlantıyı bakın.* yol değişkeni url ve kodda bir hata düzeltmek için güncellendi.
Nasıl Excel VBA kullanarak bir sunucu ...
Nasıl Http isteği json veri NSURLReque...
Nasıl CURL ile veri belirtilen POST is...
Nasıl terminalden Curl ile JSON veri g...
Nasıl ayrıştırma kullanarak Node.js JS...